Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Add all elements to a hash set and return its size. Duplicates collapse automatically. O(n) time, O(n) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you use a set for uniqueness and know the sort-based O(1)-space alternative.
Where candidates slip
Counting length, not distinct values.
How to stand out
State the time/space trade between the set and the sort-then-scan approaches.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Hash set size
1Hint 1Explore hintHide hint
What structure automatically ignores duplicate insertions?
2Hint 2Explore hintHide hint
The set's size is your answer — not the array length.
3Hint 3Explore hintHide hint
No extra space allowed? Sort and count adjacent changes instead.
A · Core idea
Insert every element into a hash set and read its size — the set stores each value at most once, so its size is the count of distinct values. O(n) time, O(n) space.
Why a set collapses duplicates. Re-inserting a value already present is a no-op, so after inserting the whole array the set contains exactly the unique values. The answer is just set.size() — no manual de-duplication.
The O(1)-space alternative. Sort the array, then count positions where an element differs from its predecessor. That's O(n log n) time but O(1) extra space — the classic time-vs-space trade you should name.
Don't confuse size with length. Returning nums.length counts every element including repeats; you want the set's size.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 2, 3, 3, 3]1,2
insert
{1,2}3C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(n) space — each insert is O(1) average, so O(n) time; the set may hold all n values, so O(n) space.
E · Edge cases and pitfalls4nums = [5, 5, 5] — One distinct value → 1.Explore edge casesHide edge cases
Edge cases handled
nums = [5, 5, 5]One distinct value → 1.nums = []Empty → 0.
Pitfalls
- Returning the array length instead of the set size.
- Forgetting an empty array has 0 distinct values.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int distinct(int[] nums) { java.util.Set<Integer> set = new java.util.HashSet<>(); for (int x : nums) set.add(x); return set.size(); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Counting the array length instead of the set size.
- Forgetting that an empty array has 0 distinct values.
Revise
Carry the pattern into the interview
Pattern to remember
Set for uniqueness
Code pattern to memorize
return len(set(nums))Rules to remember
- 1Insert all into a set.
- 2Size = distinct count.
- 3Sort+scan is the O(1)-space alternative.
Use this pattern when
- Counting unique values
- Membership/uniqueness
Choose another pattern when
- O(1) space mandated — sort then count adjacent diffs
Interview takeaway
Uniqueness counting is a one-liner with a set.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Set for uniqueness. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Contains DuplicateComing soonDistinct count < length means a duplicate exists.
- Frequency of ElementsA map gives counts as well as distinctness.