Algorithms and Data Structures · Junior · Question 16 of 75

What is the simplest way to remove duplicates from an array of primitives?

const arr = [3, 1, 3, 2, 1];

Explanation

A Set keeps only unique values and preserves the order of first occurrence, so the result is [3, 1, 2] in O(n). Sorting doesn't remove anything, and filter(Boolean) only drops falsy values.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 16

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz