Start learning free

LESSON 234 OF 291 · SETS

Unique Count in Scala: Counting Distinct Values with a Set

Practice Read the full explanation

Given an array of integers nums, return how many different values it has.

A value that appears several times counts only once.

 

Example 1:

Input: nums = [4, 7, 4, 1, 7]
Output: 3
Explanation: The different values are 4, 7 and 1. The second 4 and the second 7 are repeats, so they don't count again.

Example 2:

Input: nums = [5, 5, 5]
Output: 1
Explanation: Every number is 5, so there is only one different value.

Example 3:

Input: nums = [-2, 0, 3, 8]
Output: 4
Explanation: No value repeats, so every number counts.

 

Constraints:

  • nums has at least one number.
  • Every number is between -109 and 109.
STEP 1 OF 2
    Ctrl + Enter to check

    SCALA · SETS

    Unique Count in Scala, explained

    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

    1. Build the set. scala.collection.mutable.Set[Int](nums: _*) creates a mutable set of Int from the values of nums. The : _* after nums passes the array's elements one by one, as if they were typed out as separate arguments. Store it in a val named unique.
    2. Return the size. unique.size is 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 that x: _* is no longer supported and suggests x*, 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 as HashSet(4, 7). The set's own add method returns true when the value was new and false when 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.size and nums.distinct.length both give 3 for Array(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 List for the unique values. A list keeps duplicates; the set is what removes them.

    Testing

    • Array(4, 7, 4, 1, 7) gives 3.
    • Array(5, 5, 5) gives 1.
    • Array(-2, 0, 3, 8) gives 4.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free