Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Count Unique (Set size)

Data Structures, Operators & Stringseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 123: New Operations to Make Sets Useful!

Implement `countUnique(arr)` returning how many distinct values it contains.

Sample tests

Input: countUnique([1,1,2])
Output: 2
Input: countUnique([])
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • arr.length counts duplicates too.
Visualize this concept: Maps and Sets →
Approach & explanation (try first)

The size of a Set built from the array is the number of distinct values.

Loading...
⌘/Ctrl + Enter

Run your code to see results.