Start learning free

LESSON 234 OF 291 · SETS

Unique Count in Kotlin: Counting with a HashSet

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

    KOTLIN · SETS

    Unique Count in Kotlin, explained

    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

    1. Build the set. nums.toHashSet() creates a HashSet that holds the values of nums. Store it in unique.
    2. Return its size. unique.size is 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 on IntArray and returns a HashSet<Int>. Printing intArrayOf(4, 7, 4).toHashSet() gives [4, 7].
    • size, not length. Collections have a size property. Arrays have size as well, and strings have length.
    • Another route. nums.toSet().size and nums.distinct().size give 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] gives 1.
    • All different. [-2, 0, 3, 8] gives 4: 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] gives 2.

    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] gives 3.
    • [5, 5, 5] gives 1.
    • [-2, 0, 3, 8] gives 4.
    • [4, 7, 4] gives 2.
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free