Lesson 1 of 8

Removing duplicates

Goal

A one-line Set solution, then the harder case

01

Explanation

Removing duplicates from simple values is easy: [...new Set(arr)] is all it takes, because a Set won't hold the same value twice, and it keeps each value in the order it first appeared.

Objects are a different story. Two objects with the same contents are still two different references, so a Set sees them as different. The fix is to pick a key that identifies each object and build a Map keyed by it.

A Map keeps insertion order too, so the result comes out in the same order as the original items.

02

Your task

Write unique(arr) for simple values and uniqueBy(arr, key) for objects.

Checks

5 checks
  1. unique removes duplicates
  2. unique keeps the order of first appearance
  3. An empty array returns an empty array
  4. uniqueBy removes duplicate objects by key
  5. uniqueBy keeps the first occurrence, not the last
03

Hints

0/3
Track lessons0 of 8 done

Write functions for searching, sorting, grouping and flattening nested lists once, then reuse them in every project after that.

Advanced level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

Write functions for searching, sorting, grouping and flattening nested lists once, then reuse them in every project after that.

Advanced level, 8 lessons, 1.5 hours in total.