JS Built-ins
← Array13 / 226
ArrayArray.fromintermediate

Array.from() β€” deduplicate with a Set

πŸ“– Usage Example
const set = new Set([1, 2, 2, 3, 3])
Array.from(set)  // β†’ [1, 2, 3]

Use Array.from() to convert a Set into an array, removing duplicates in the process.

Array.from() β€” converting a Set

A Set only holds unique values. By converting an array to a Set and back with Array.from(), you can remove duplicates in one expression. Set is iterable, so Array.from() works directly on it.

Challenge: Implement removeDuplicates that removes duplicate numbers from an array.

removeDuplicates([1, 2, 2, 3, 3, 3]) // β†’ [1, 2, 3]
removeDuplicates([])                  // β†’ []
πŸ’‘ Hints (2)
  • β€’ `new Set(arr)` creates a Set from the array, automatically dropping duplicates.
  • β€’ `Array.from(set)` converts the Set back into a plain array.