> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Remove Duplicates from Sorted Array

Leetcode Problem 26: Remove Duplicates from Sorted Array

Problem Summary

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Return k after placing the final result in the first k slots of nums.

Techniques

  • Two-pointer technique
  • In-place modification

Solution

function removeDuplicates(nums: number[]): number {
    let leftLimit = 0

    for (let current = 1; current < nums.length; current++) {
        if (nums[leftLimit] !== nums[current]) {
            nums[++leftLimit] = nums[current]
        }
    }

    return leftLimit + 1
};

console.log(removeDuplicates([0,0,1,1,1,2,2,3,3,4]))