Start learning free

LESSON 233 OF 289 · SETS

Unique Count in C#: Counting Distinct Values with 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

    C# · SETS

    Unique Count in C#, explained

    Problem overview

    The method UniqueCount receives an int[] named nums and returns how many different values it holds. A value that appears several times counts once.

    For [4, 7, 4, 1, 7] the answer is 3: the different values are 4, 7 and 1. For [5, 5, 5] it is 1, and for [-2, 0, 3, 8] it is 4, because nothing repeats. The array has at least one number, and the numbers are between -1,000,000,000 and 1,000,000,000, so they fit in an int.

    Approach: let a set remove the repeats

    A set is a collection that holds each value at most once. Adding a value that is already inside changes nothing. Checking whether a value is in the set is fast. C#'s set type is HashSet<T>.

    That is exactly the tool for this problem. Put all the numbers into a HashSet<int>, and the repeats collapse by themselves. The number of values left in the set is the number of different values in the array.

    A HashSet can be built straight from an array: new HashSet<int>(nums) creates a set that starts with the array's values. No loop is needed. For [4, 7, 4, 1, 7] the set ends up holding 4, 7 and 1.

    Without a set, you would need a second collection and a loop: for each number, check whether it was already collected, and add it if not. You would then count the collected values. The set does the checking inside Add and in the constructor, so the whole method shrinks to two lines. Sorting the array and counting the places where the value changes is another way, but it needs more code and reorders data you may want to keep.

    It is also fast. Building the set visits each number once, and each insertion takes constant time on average, so the work grows in step with the length of the array, O(n). The set holds at most one entry per distinct value, so the extra space is O(k) for k distinct values.

    Step by step

    1. Create the set: HashSet<int> unique = new HashSet<int>(nums);. It starts with the values of nums, each one once.
    2. Return its size, unique.Count. A set reports its size with Count, not Length.

    The solution in C#

    class Solution {
        public int UniqueCount(int[] nums) {
            HashSet<int> unique = new HashSet<int>(nums);
            return unique.Count;
        }
    }

    Details worth knowing:

    • A set has no indexes. You can't write unique[0], and you shouldn't count on any particular order when you go over it.
    • The Add method of a HashSet returns a bool: true if the value was new and false if it was already there. Adding 4 to a set that holds 4 gives false, and adding 7 gives true.
    • Arrays use Length, lists and sets use Count. Writing unique.Length doesn't compile.

    Edge cases and common mistakes

    • Returning nums.Length. That counts every element, repeats included.
    • Using unique.Length. A HashSet has Count.
    • All the same value. [5, 5, 5] gives a set with one value, so the answer is 1.
    • Negative numbers and zero. They are ordinary values and count like any other.

    Testing

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