Start learning free

LESSON 235 OF 292 · SETS

Unique Count in Swift: 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

    SWIFT · SETS

    Unique Count in Swift, explained

    Problem overview

    The function uniqueCount receives an array of integers, nums, and returns how many different values it contains. 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], where nothing repeats, 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, and checking whether a value is in a set is fast. Building a set from an array therefore drops every repeat, and the set's size is the number of different values.

    The alternative, comparing every number with every other, would take time proportional to the square of the array's length. The set avoids that by remembering the values it has seen.

    Step by step

    1. Build the set with Set(nums) and store it in a constant named unique. It is a let because it doesn't change afterwards.
    2. Return its size with unique.count.

    On [4, 7, 4, 1, 7], the set grows like this as the values are added: 4 gives one member, 7 gives two, the second 4 changes nothing, 1 gives three, and the second 7 changes nothing. The set ends with three members, and count returns 3.

    The same idea answers related questions: whether an array has any duplicate (compare Set(nums).count with nums.count), or which values two arrays have in common. A set is the tool to reach for whenever "have I seen this before?" is the question.

    The solution in Swift

    class Solution {
        func uniqueCount(_ nums: [Int]) -> Int {
            let unique = Set(nums)
            return unique.count
        }
    }
    

    Swift details

    • Type inference. Set(nums) is a Set<Int>, because nums is an [Int]. You don't need to write the element type.
    • Elements must be Hashable. Int, String and Character are. A custom type needs to conform to Hashable before it can go in a set.
    • A set has no order. This lesson only needs its size. Don't rely on the order in which a set lists its values.
    • Everything in one line. The two lines can be written as return Set(nums).count. The lesson uses the named constant so each step is visible.
    • Negative numbers and zero are ordinary values here: [-2, 0, 3, 8] has four different ones.

    Complexity

    Time: O(n) on average. Building the set visits each of the n numbers once, and each insertion takes constant time on average.

    Space: O(k) for k different values, which is at most n.

    Edge cases and common mistakes

    • All equal. [5, 5, 5] gives 1.
    • All different. The answer equals the array's length: [-2, 0, 3, 8] gives 4.
    • Returning nums.count. That counts every element, including repeats, so [4, 7, 4, 1, 7] would give 5 instead of 3.
    • A single number. The answer is always 1.
    • Large values. Values up to a billion in size are ordinary Int values, since an Int in Swift has 64 bits.

    Testing

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