Problem overview
The function uniqueCount receives an IntArray, nums, and returns how many different values it has. A value that appears several times counts once. For [4, 7, 4, 1, 7] the different values are 4, 7 and 1, so the answer is 3. For [5, 5, 5] it is 1, and for [-2, 0, 3, 8] it is 4.
Approach: let a set remove the repeats
A set holds each value at most once: adding a value that is already inside changes nothing. Put every number of nums into a set, and the repeats disappear by themselves. The set's size is then the number of different values. Checking whether a value is in a set is also fast, which is why a set beats comparing every pair of numbers.
Step by step
- Build the set.
nums.toHashSet()creates aHashSetthat holds the values ofnums. Store it inunique. - Return its size.
unique.sizeis the number of values in the set, and all of them are different.
The solution in Kotlin
class Solution {
fun uniqueCount(nums: IntArray): Int {
val unique = nums.toHashSet()
return unique.size
}
}
Kotlin details worth knowing:
- Conversion functions.
toHashSet()is defined onIntArrayand returns aHashSet<Int>. PrintingintArrayOf(4, 7, 4).toHashSet()gives[4, 7]. size, notlength. Collections have asizeproperty. Arrays havesizeas well, and strings havelength.- Another route.
nums.toSet().sizeandnums.distinct().sizegive the same count.toSet()keeps the first-seen order, which doesn't matter for a count.
A set at work
For [4, 7, 4, 1, 7], the set built by toHashSet() prints as:
[1, 4, 7]
Five numbers went in and three values are in the set, so the answer is 3. Notice that the set prints as 1, 4, 7, not in the order the numbers arrived: a HashSet places values by their hash and promises no particular order. For counting, order is irrelevant, but don't rely on it elsewhere.
Why a set and not a list
You could keep a list of the values seen and check it for each number, but checking a list means looking at its items one by one, so the total work grows with the square of the array's length. A hash set finds a value in constant time on average, which is the reason it's the tool for questions that begin "have I seen this value before?"
Complexity
Time: O(n). Adding each number to a hash set takes constant time on average.
Space: O(k), where k is the number of different values, at most n.
Edge cases and common mistakes
- All equal.
[5, 5, 5]gives1. - All different.
[-2, 0, 3, 8]gives4: the size of the input. - Returning
nums.size. That counts every element, repeats included. - Counting the values that repeat. The answer counts each different value once, whether it appears once or many times:
[4, 7, 4]gives2.
Reading a set's size
A set has no positions, so there is no unique[0]. You can ask how many values it holds with size and whether it holds a value with contains, and you can loop over it. Those three operations cover most of what a set is used for, and this lesson needs only the first.
Testing
[4, 7, 4, 1, 7]gives3.[5, 5, 5]gives1.[-2, 0, 3, 8]gives4.[4, 7, 4]gives2.
