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];- 1
arr.sort().reverse() - ✓Correct answer:
[...new Set(arr)] - 3
arr.filter(Boolean)
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.