All of us looking for software development jobs know that practicing algorithms and coding questions is very important. So, I started solving coding questions on a popular website — LeetCode.

I figured it would be a great practice for me to solve those questions, and then try to explain each challenge here. So, I’ll start with the most recent one I did — Remove Duplicates from a Sorted Array.

I have to warn readers that my solutions won’t necessarily be the fastest ones. But I always try to come up with my own solutions, rather than finding one online.

Question

Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Input: nums = [1,1,2]

Output: 2, nums = [1,2]

Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the returned length.

#javascript

How to Remove Duplicates from a Sorted Array
1.15 GEEK