Coding challenge Day-2

Coding challenge Day-2

Second day of I4G coding challenge

Remove Element

Question

Used JavaScript for the challenge

Today's challenge was pretty straightforward. This was so because I took note of all my mistakes during yesterday's challenge.

Thought process

Today's challenge was a little bit similar to yesterday's challenge so my approach was the same. These were my thought process:

  1. Loop through the input array (nums) and compare each number in the array (nums) with the input integer (val).

  2. If the two values are the same, it will be skipped but if they're not the same, the number from the input array (num), it will be pushed to a new array (newArr).

  3. Empty the input array(nums) and spread the new array (newArr) into the empty input array(nums). This step is to happen because the function will have to return the input array(nums).

Implementation

These are the steps I used to implement my thought process:

  1. Created a new empty array(newArr).

  2. Used map() to loop through the nums array.

  3. Used an if block to check if the current number n is equal to the input integer val. If not equal, I used the push() method to push it into the new array (newArr) created from the start.

  4. Used the splice() method to empty the input array (nums)

  5. Used the unshift() method and spread (...) operator to add the numbers in the newArr array to the input nums array

What I learnt

Didn't learn anything new, it was more about getting comfortable approaching leetcode questions

Conclusion

Answer

Today's task was fun to work on and I enjoyed making use of many array methods again like yesterday's task. The runtime of my code was 93ms and memory usage of 42.3MB

Thank you for reading through!

Day two(2) done, Eight(8) more days to go!

You can follow me on Twitter