Good morning! Here's your coding interview problem for today.
This problem was asked by Tencent.
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. Then return the number of unique elements in nums.
Example
function removeDuplicates(nums){
}
console.log(removeDuplicates([1, 2, 3, 1, 3]))
// [1, 2, 3]
Solution
function removeDuplicates(nums) {
const set = new Set();
for (let i = 0; i < nums.length; i++) {
set.add(nums[i]);
}
let k = 0;
for (const num of set) {
nums[k++] = num;
}
nums.length = k;
return k;
}
Explanation
- Use a Set to collect only unique elements from the unsorted array.
- Loop over the original array and add each element to the Set.
- Set will only contain unique elements, discarding duplicates.
- Loop over Set to rebuild array with only uniques in original order.
- set size of array to k
- Return k which is the new length after removing duplicates.
Top comments (2)
Hi Chandra,
Using the Set data type is an excellent way to remove duplicates and can be created directly from an array as follows.
The set can them be turned back into a simple array using its
keys
method, as follows.This can greatly simplify the example above but does not explain the how it works anywhere near as well.
Another, more complicated, approach is to use the
Array.reduce
method, as follows.I accept neither of the above approaches are 'in-place' but this can be resolved using the following script.
Best regards, Tracy
Chandra,
Your example needs to return
nums
notk
otherwise the output would be the number 3 and not the original array.Your post certainly had me thinking, so here is an in-place alternative.
Tracy