Find the least frequent element in an array using Python

Finding the least frequent element in an array is a common programming problem. Python offers several approaches: sorting with linear traversal, hashing with dictionaries, and using the Counter class from the collections module.

Methods Overview

The following methods can accomplish this task ?

  • Using sort() function (Naive Approach)

  • Using Hashing with Dictionary

  • Using Counter() Function

Method 1: Using sort() Function

This approach first sorts the array, then traverses it linearly to count frequencies. The time complexity is O(n log n) due to sorting.

Example

The following program returns the least frequent element using the sort() function ?

def leastFrequencyElement(inputList, listLength):
    # Sort the input list
    inputList.sort()
    
    # Initialize minimum count and result
    minimumCount = listLength + 1
    resultElement = -1
    currentCount = 1
    
    # Traverse the sorted list
    for k in range(1, listLength):
        if inputList[k] == inputList[k - 1]:
            # Same element, increment count
            currentCount += 1
        else:
            # Different element, check if current count is minimum
            if currentCount < minimumCount:
                minimumCount = currentCount
                resultElement = inputList[k - 1]
            currentCount = 1
    
    # Check the last element group
    if currentCount < minimumCount:
        minimumCount = currentCount
        resultElement = inputList[listLength - 1]
    
    return resultElement

# Input array
numbers = [6, 5, 5, 4, 4, 2, 2, 2, 1, 1]
print("Given List:", numbers)

listLength = len(numbers)
result = leastFrequencyElement(numbers, listLength)
print("Least frequent element:", result)
Given List: [6, 5, 5, 4, 4, 2, 2, 2, 1, 1]
Least frequent element: 6

Method 2: Using Dictionary (Hashing)

This method uses a dictionary to store element frequencies, then finds the key with minimum value. Time complexity is O(n).

Example

The following program uses hashing to find the least frequent element ?

def leastFrequencyElement(inputList):
    # Create frequency dictionary
    frequency = {}
    
    # Count frequencies
    for element in inputList:
        frequency[element] = frequency.get(element, 0) + 1
    
    # Find element with minimum frequency
    minimumCount = min(frequency.values())
    
    # Return the first element with minimum frequency
    for element, count in frequency.items():
        if count == minimumCount:
            return element

# Input array
numbers = [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
print("Given List:", numbers)

result = leastFrequencyElement(numbers)
print("Least frequent element:", result)
Given List: [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
Least frequent element: 10

Method 3: Using Counter() Function

The Counter class from collections module provides an elegant solution by automatically counting element frequencies.

Example

The following program uses Counter() to find the least frequent element ?

from collections import Counter

def leastFrequencyElement(inputList):
    # Get frequency count of all elements
    frequency_counter = Counter(inputList)
    
    # Find minimum frequency
    minimumCount = min(frequency_counter.values())
    
    # Return element with minimum frequency
    for element, count in frequency_counter.items():
        if count == minimumCount:
            return element

# Input array
numbers = [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
print("Given List:", numbers)

result = leastFrequencyElement(numbers)
print("Least frequent element:", result)
Given List: [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
Least frequent element: 10

Comparison

Method Time Complexity Space Complexity Best For
Sorting O(n log n) O(1) Memory-constrained scenarios
Dictionary O(n) O(n) General purpose
Counter O(n) O(n) Clean, readable code

Conclusion

Use Counter for clean and readable code, dictionary hashing for general-purpose solutions, and sorting when memory is limited. The dictionary and Counter methods offer better time complexity at O(n) compared to sorting at O(n log n).

Updated on: 2026-03-26T23:50:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements