Problem overview
The method uniqueCount receives an Array[Int] named nums and returns how many different values it holds. A value that appears several times counts once.
For Array(4, 7, 4, 1, 7) the different values are 4, 7 and 1, so the answer is 3. For Array(5, 5, 5) it is 1, and for Array(-2, 0, 3, 8), where nothing repeats, it is 4.
Approach
A set is a collection that holds each value at most once. Adding a value that is already in it changes nothing, and checking membership is fast. So if you put every number into a set, the repeats collapse by themselves, and the set's size is the answer. There is no loop to write and no bookkeeping about what was seen.
The brute-force alternative compares every number with every other, which takes O(n²) time. Building a set takes one pass.
Step by step
- Build the set.
scala.collection.mutable.Set[Int](nums: _*)creates a mutable set ofIntfrom the values ofnums. The: _*afternumspasses the array's elements one by one, as if they were typed out as separate arguments. Store it in avalnamedunique. - Return the size.
unique.sizeis the number of values the set holds, and every one of them is different.
The solution in Scala
class Solution {
def uniqueCount(nums: Array[Int]): Int = {
val unique = scala.collection.mutable.Set[Int](nums: _*)
return unique.size
}
}
- Varargs splice.
Set[Int](1, 2, 3)takes any number of values. To pass an existing array in their place, you splice it. In Scala 3.5 the compiler prints a warning thatx: _*is no longer supported and suggestsx*, but the code still compiles and runs correctly, and the lesson's tests accept it.scala.collection.mutable.Set[Int](nums*)is the newer spelling and behaves the same way. - Duplicates vanish. A set stores a value once, so
mutable.Set(4, 7, 4)prints asHashSet(4, 7). The set's ownaddmethod returnstruewhen the value was new andfalsewhen it was already there. - Order isn't kept. A hash set doesn't promise an order. Only the size matters here.
- Library shortcuts.
nums.toSet.sizeandnums.distinct.lengthboth give 3 forArray(4, 7, 4, 1, 7). They do the same work with a different collection.
Sets, lists and maps
A List or an Array remembers position and keeps repeats. A set remembers only membership: a value is in it or it isn't. A map attaches a value to each key, and its keys behave like a set. Choosing a set whenever the question is "is it in there?" or "how many different?" keeps the code short, and it is the same idea the next lessons use to detect repeats.
Complexity
Time: O(n) on average. Each of the n numbers is hashed and added once, and adding 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 the same.
Array(5, 5, 5)gives 1. - No repeats.
Array(-2, 0, 3, 8)gives 4. Negative numbers and 0 count like any other value. - Returning
nums.length. That counts every element, repeats included. - Using a
Listfor the unique values. A list keeps duplicates; the set is what removes them.
Testing
Array(4, 7, 4, 1, 7)gives3.Array(5, 5, 5)gives1.Array(-2, 0, 3, 8)gives4.
