5 Best Ways to Find the Number of Unique Integers in a Sorted List in Python

Rate this post

πŸ’‘ Problem Formulation: You have a list of integers that is already sorted and you need to find out how many unique numbers are present in that list. For example, given the input list [1, 2, 2, 3, 3, 3, 4], the desired output is 4 since there are four unique integers (1, 2, 3, and 4).

Method 1: Using a for-loop

This method involves iterating over the sorted list and counting unique integers by comparing adjacent elements. The function specification would involve taking a list as input and returning an integer representing the count of unique numbers.

Here’s an example:

def unique_count(sorted_list):
    if not sorted_list:
        return 0
    count = 1
    for i in range(1, len(sorted_list)):
        if sorted_list[i] != sorted_list[i-1]:
            count += 1
    return count

print(unique_count([1, 2, 2, 3, 3, 3, 4]))

Output: 4

This code snippet first checks if the list is empty. If it isn’t, it initializes a counter at one (to account for the first unique element) and then continues to loop through the list, incrementing the counter whenever it finds a new unique element by comparing it with the previous one.

Method 2: Using set()

The set() function in Python is a built-in function that can be used to create a set, which is a collection of distinct elements. By converting the sorted list into a set, we can simply count the number of items in that set to know the number of unique integers.

Here’s an example:

def unique_count(sorted_list):
    return len(set(sorted_list))

print(unique_count([1, 2, 2, 3, 3, 3, 4]))

Output: 4

This snippet converts the sorted list into a set, eliminating any duplicate values. The length of the resulting set is the number of unique integers since a set can only contain distinct values.

Method 3: Using itertools.groupby()

The itertools.groupby() function is a useful tool from the itertools library that groups adjacent list items together. When used on a sorted list, each group will consist of identical elements, and the number of groups will give the number of unique elements.

Here’s an example:

from itertools import groupby

def unique_count(sorted_list):
    return sum(1 for _ in groupby(sorted_list))

print(unique_count([1, 2, 2, 3, 3, 3, 4]))

Output: 4

This function works by using groupby to group the sorted list by consecutive identical elements. It uses a generator expression to count the groups, which corresponds to the number of unique elements in the list.

Method 4: Using numpy.unique()

For those working with numerical data in Python, numpy offers a convenient function called numpy.unique() which finds the unique elements of an array. When called with the ‘return_counts’ argument, it returns the unique values and the number of times each unique value appears in the array.

Here’s an example:

import numpy as np

def unique_count(sorted_list):
    uniques = np.unique(sorted_list)
    return len(uniques)

print(unique_count([1, 2, 2, 3, 3, 3, 4]))

Output: 4

The numpy.unique() function is particularly fast and efficient, especially for large datasets. By simply taking the length of the array returned by numpy.unique(), we get the count of unique numbers in the original list.

Bonus One-Liner Method 5: Using List Comprehension with slicing

A one-liner solution that combines list comprehension and slicing. It works by comparing each element with its successor and counts as unique if it’s different.

Here’s an example:

sorted_list = [1, 2, 2, 3, 3, 3, 4]
unique_count = 1 + sum(1 for i in range(len(sorted_list) - 1) if sorted_list[i] != sorted_list[i + 1])
print(unique_count)

Output: 4

The one-liner uses list comprehension to iterate over the list indices, comparing each element with the next. It adds one for each unique instance it finds, and includes the first element in the list by starting the total at one.

Summary/Discussion

  • Method 1: For-loop. Strengths: Simple, no external libraries, works on any iterable. Weaknesses: Potentially slow for very large lists.
  • Method 2: Using set(). Strengths: Very concise and easy to understand. Weaknesses: Additional memory used to create the set.
  • Method 3: Using itertools.groupby(). Strengths: Part of the standard library, efficient for large lists. Weaknesses: slightly less intuitive than other methods.
  • Method 4: Using numpy.unique(). Strengths: Extremely fast for array operations, great for numerical data. Weaknesses: Requires numpy, not part of the standard library.
  • Method 5: One-liner with List Comprehension. Strengths: Compact syntax, no additional space required. Weaknesses: Might be less readable to beginners.