Start learning free

LESSON 236 OF 671 · SETS

Remove Repeats in JavaScript with a Set

Practice Read the full explanation

You get an array of integers nums. Some values may appear more than once.

Return a new array that has each value only once.

Keep the first copy of each value and drop the later ones. The values stay in the order they first appear in nums.

 

Example 1:

Input: nums = [3, 1, 3, 2, 1]
Output: [3, 1, 2]
Explanation: The second 3 and the second 1 are repeats.
They are dropped, and 3, 1 and 2 stay in their order.

Example 2:

Input: nums = [6, 6, 6, 6]
Output: [6]
Explanation: Only the first 6 stays.

Example 3:

Input: nums = [4, 9, 2]
Output: [4, 9, 2]
Explanation: No value repeats, so every number stays.

 

Constraints:

  • nums has at least one number.
  • Every number is between -109 and 109.
STEP 1 OF 7
    Ctrl + Enter to check

    JAVASCRIPT · SETS

    Remove Repeats in JavaScript, explained

    Problem overview

    The method removeRepeats gets an array of integers, nums, and returns a new array in which each value appears only once. It keeps the first copy of each value and drops the later ones, so the values stay in the order they first appear.

    For [3, 1, 3, 2, 1] the answer is [3, 1, 2]: the second 3 and the second 1 are repeats. For [6, 6, 6, 6] the answer is [6]. For [4, 9, 2] nothing repeats, so every number stays.

    Approach

    Read the numbers from left to right, and remember which values have been met. A number that isn't remembered yet is a first copy: put it in the result and remember it. A number that is already remembered is a repeat: skip it.

    A Set is the right place to remember values, because has answers "have I seen this value?" quickly, however many values are stored. Reading from left to right is what keeps the first copy and the original order.

    Step by step

    1. Create seen, an empty set, with new Set().
    2. Create result, an empty array that collects the values to return.
    3. Loop over nums with for (let num of nums), from the first number to the last.
    4. Check for a first copy: !seen.has(num) is true when num isn't in the set. The ! turns the answer around.
    5. Push num onto result. Adding at the end keeps the order of first appearance.
    6. Add num to seen, so a later copy is skipped.
    7. Return result after the loop, not inside it, or only the first value would come back.

    The solution in JavaScript

    class Solution {
        removeRepeats(nums) {
            let seen = new Set();
            let result = [];
            for (let num of nums) {
                if (!seen.has(num)) {
                    result.push(num);
                    seen.add(num);
                }
            }
            return result;
        }
    }
    

    The array result is the output, and the set seen is only a memory. Keeping them separate matters: a set could hold the values too, but an array is what the method must return.

    Why a set and not the result array

    You could ask the result array itself, result.includes(num), and it gives the same answer, [3, 1, 2] for the first example. The difference is speed. includes looks at the stored values one by one, so a long array with many different values makes every check slower. A set finds a value without scanning the others, which is why the lesson uses one.

    Another way

    Because a set already keeps each value once, and keeps them in the order they were added, spreading a set into an array does the same job in one line:

    function removeRepeatsShort(nums) {
        return [...new Set(nums)];
    }
    

    It returns [3, 1, 2], [6] and [4, 9, 2] on the three examples. The longer version is the one the lesson's steps build, and it shows what the set is doing.

    Testing

    On the statement's examples, the lesson's solution gives these lines, with the values separated by spaces:

    3 1 2
    6
    4 9 2
    

    Common mistakes

    • Returning the set's size. new Set(nums).size is the number of different values, 3 for the first example, not the array.
    • Sorting first. [3, 1, 3, 2, 1].sort() gives [1, 1, 2, 3, 3], which loses the order of first appearance. Also, with no comparator, sort() sorts numbers as text: [10, 9, 1, 10].sort() gives [1, 10, 10, 9].
    • Returning inside the loop. The method would stop after the first value.
    • Adding to seen outside the if. That is harmless here, but pushing to result outside the if keeps every repeat.
    Keep going with JavaScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free