DEV Community

Achilonu Chinwendu Faustina
Achilonu Chinwendu Faustina

Posted on

14G10DaysCodeChallenge: Day 1(Remove Duplicates from Sorted Array)

Two Pointers were applied in this challenge, the first pointer[j] which is used to iterate through the given array of numbs and second pointer [i] which is used in placing a unique value to the position after it.

Procedure

  • Return 0, if nums.length = 0; which means there will be no elements in the array.

  • Start j with the value of 1.

  • Using the for loop, set i = 0; i < nums.length - 1 (we subtract 1 because once we get to the last value of array, we will not have any value to compare with).

  • On each iteration, there is comparison between the current value of nums[i] with the next value of nums[i + 1].

  • Using the if statement, compare[i] with [i + 1], if they are not equal, set nums of [j] equals to nums of [i + 1] and increment j.

  • Return j.

Top comments (0)