DEV Community

Cover image for Day 2 of 10days coding challenge: Remove element
Ezekiel nwuguru
Ezekiel nwuguru

Posted on

Day 2 of 10days coding challenge: Remove element

Hey! I just completed my day 2 challenge for 10days coding challenge with Ingressive4good. Today's task was to write a code that removes a given element from an array.

Here's my approach:

  • I need two arrays to achieve this, the first one is the given array while the second one is the new array without the elements.

  • I need two integers, the first to iterate through the given array while the second to generate the length of the new array

  • I need to check through the given array and return new array with the given value.

Algorithm:

  1. Initialize integer i to zero and declare integer k

  2. Use for loop to iterate through nums array with condition: k < nums.length

  3. check if each value of the array is not equal to the given element.

  4. If not equal set nums[i] = nums[k] and increment i

  5. Return the value of i.

Check out the code here Remove Element - LeetCode https://leetcode.com/problems/remove-element/submissions/

Top comments (0)