Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Remove Duplicates

Working With Arrayseasy

Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods

Implement `unique(arr)` returning the array with duplicates removed, keeping the first occurrence order.

Sample tests

Input: unique([1,2,2,3,1])
Output: [1,2,3]
Input: unique([])
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Object-valued elements are compared by reference, so a Set will not dedupe equal-looking objects.

Learning resources

  • MDN: Array
Approach & explanation (try first)

new Set keeps first occurrences in order; spreading gives a deduplicated array.

Loading...
⌘/Ctrl + Enter

Run your code to see results.