Skip to curriculum
Jump to the editor

4.7. Remove duplicates (values and by key)

Easy
Topics

This question is in

4. Array utilities
10–20 min

A list of tags typed by users has "css" three times. Merging two pages of search results repeats the products that were on both pages. A list of recent files should show each file once, keeping the most recent position. Removing duplicates is one of the most common array tasks, and the details matter: which copy is kept, what counts as "the same", and whether it stays fast for long lists.

Write uniq(array), which keeps each value once, and uniqBy(array, iteratee), which keeps the first item for each key, in uniq.js.

Signature

JAVASCRIPT
function uniq(array)             // returns a new array
function uniqBy(array, iteratee) // returns a new array
  • iteratee gives each item's key: a function, called as iteratee(item), or a property name such as "id", which means item.id.
  • Both return a new array of items from array, in the order they first appeared. array is not changed.

Requirements

  1. uniq keeps the first occurrence of each value, in the order of first appearance.
  2. Values are compared the way a Set compares them: NaN matches NaN, 0 matches -0, and nothing else is converted (1 and "1" are different).
  3. Of 0 and -0, the one that comes first is kept as it was.
  4. Objects match only when they are the same object, not when they look alike.
  5. undefined and null are kept once each. A hole counts as undefined.
  6. An empty array gives []. The result is always a new array, and the input is not changed.
  7. uniqBy keeps the first item for each key and returns the items, not the keys.
  8. uniqBy accepts a function or a property name, and calls the iteratee once per item, with the item.
  9. uniqBy compares keys the same way uniq compares values.
  10. Both handle 100,000 items quickly.

Examples

JAVASCRIPT
uniq([3, 1, 3, 2, 1]);          // [3, 1, 2]
uniq([NaN, 1, NaN, "1"]);       // [NaN, 1, "1"]
JAVASCRIPT
uniqBy([2.1, 1.2, 2.3], Math.floor);   // [2.1, 1.2]
uniqBy([{ id: 1, v: "a" }, { id: 2, v: "b" }, { id: 1, v: "c" }], "id");
// [{ id: 1, v: "a" }, { id: 2, v: "b" }]
JAVASCRIPT
uniq([{ a: 1 }, { a: 1 }]);     // both kept: two different objects
uniq([]);                       // []

Environment

  • Plain JavaScript in a browser page; the tests call the globals uniq and uniqBy from uniq.js.
  • Any built-in may be used.
  • One test runs both functions on 100,000 different values and expects them to finish in well under a second.

Hints

  1. Checking "have I seen this?" by searching the result array gets slower as the result grows. What remembers values with a fast lookup?
  2. A Set stores -0 as 0. If you build the result from the Set, which value comes out?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…