Coding challenge Day-1

Coding challenge Day-1

A Coding challenge from Ingressive for Good (I4G)

ยท

2 min read

Removing duplicates from a sorted array

Question

Heading into the challenge, I had to first make a language pick on leetcode, and of course, I went for JavaScript ๐Ÿ˜…. The challenge was to remove duplicate numbers from an array(nums). Looked straightforward but when I started, I was shocked when I didn't get the answer on the first attempt.

N.B: You might notice throughout these articles for the coding challenge that the solutions weren't given, but just the thought process and how it was implemented in words. This is because of the instruction by the organizers not to include the solutions in the articles.

Thought process

Seeing the question, I knew I have to do the following:

  1. Loop through the array(nums).

  2. Set a condition that checks if the element is already in the new array(newArr).

  3. If it is there, Pass

  4. If it isn't, push each of the arrays into an initially empty array(newArr).

  5. Replace the content of the initial array(nums) with the content of the new array(newArr).

Implementation

After processing the question and I started to implement it, it was war at first because I had to use the right array methods to achieve this:

  1. Used map() method to loop through the original array(nums).

  2. Used the includes() method to conditionally check if the new array(newArr) contained the element I wanted to push.

  3. Used the push() method to push the elements as I looped over them to the new array(newArr).

  4. Used splice() method to empty the original array(nums) after getting the desired unique elements in the new array(newArr).

  5. Used unshift() method to add the new array elements into the original array(nums) that was now empty.

  6. To add only the content of the new array (newArr) to the original array (nums), I used the spread operator on the new array.

What I learnt

This challenge looked simple but turned out to give me a little stress. It taught me to always try to visualize my result first by writing understandable pseudo codes. I was also able to make use of many array methods and refresh my knowledge of their uses.

Conclusion

Submission

I might not have gotten it perfectly correct but seeing the success after clicking submit was refreshing. Looking forward to doing more of leetcode challenges even after this coding challenge.

Thank you for reading through this!

Day one (1) of the coding challenge down, and Nine (9) more days to go!

This is my first ever blog๐Ÿ˜…๐Ÿ˜ƒ. Hoping for more to come!

You can follow me on Twitter

ย