# Find the least frequent element in an array using Python

In this article, we will learn the python program for getting the least frequent element in an array.

### Methods Used

The following are the various methods to accomplish this task −

• Using sort() function(Naive Approach)

• Using Hashing

• Using Counter() Function

## Method 1: Using sort() function(Naive Approach)

Running two loops is a simple fix. The outer loop selects each element one by one. The inner loop calculates the frequency of the selected element and compares it to the least value reached thus far. This solution's time complexity is O(n^2)

Sorting is a better solution. The array is sorted first, and then it is linearly traversed and we can find the least frequent element as shown in the below code.

### Example

The following program returns the least frequent element in an input array/list using the sort() function −

# creating a function for returning the least frequent element
def leastFrequencyElement(inputList, listLength):
# sorting the given input list
inputList.sort()
# Setting the minimum frequency(minimum count) as length of list + 1
minimumCount = listLength + 1
resultElement = -1
# Variable to count the frequency
currentCount = 1
# Looping from 1st index to the length of the list
for k in range(1, listLength):
# Check if the previous element is equal to the current element
if (inputList[k] == inputList[k - 1]):
#Increase the frequency of the current element by 1
currentCount = currentCount + 1
else:
# Check if the current Count is less than the minimum Count
if (currentCount < minimumCount):
#If it is true then set the minimum count as current Count value
minimumCount = currentCount
# Store this previous element as the least frequent element
resultElement = inputList[k - 1]
# Resetting the current Count as 1
currentCount = 1
# checking whether the last element is less frequent
if (currentCount < minimumCount):
minimumCount = currentCount
resultElement = inputList[listLength - 1]
# returning the least frequent element
return resultElement
# input list
inputList = [6, 5, 5, 4, 4, 2, 2, 2, 1, 1]
print("Given List is:", inputList)
# getting list length
listLength = len(inputList)
# calling the leastFrequencyElement function by passing
# input list and list length as arguments
print("Least frequent element in the input list is:")
print(leastFrequencyElement(inputList, listLength))


### Output

On execution, the above program will generate the following output −

Given List is: [6, 5, 5, 4, 4, 2, 2, 2, 1, 1]
Least frequent element in the input list is:
6


## Method 2: Using Hashing

Applying hashing is an effective solution. In this method, we create a hash table and store elements, and their frequency counts as key-value pairs. And then, we traverse the hash table and print the key with the least value.

### Example

The following program returns the least frequent element in an input array/list using hashing −

# creating a function for returning the least frequent element
# by accepting the input list and input list length as arguments
def leastFrequencyElement(inputList, listLength):
# Take a dictionary as a hash table
HashTable = dict()
# Loop in the given list
for k in range(listLength):
# Check if the list element in the hashtable
if inputList[k] in HashTable.keys():
# If it is present then increase the frequency by 1
HashTable[inputList[k]] += 1
else:
# Else create a new key with the frequency as 1
HashTable[inputList[k]] = 1
#  Setting the minimum frequency(minimumCount) as length of list + 1
minimumCount = listLength + 1
resultElement = -1
# Iterating the hashtable(dictionary)
for k in HashTable:
# Check if the minimum count is greater or equal to the frequency of the key
if (minimumCount >= HashTable[k]):
#If it is true then this key will be the current least frequent element
resultElement = k
# Set the minimum count as the current key frequency value
minimumCount = HashTable[k]

# returning the least frequent element
return resultElement
# input list
inputList = [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
# getting list length
listLength = len(inputList)
print("Given List is:", inputList)
print("Least frequent element in the input list is:")
# calling the leastFrequencyElement function by passing
# input list and list length as arguments
print(leastFrequencyElement(inputList, listLength))


### Output

On execution, the above program will generate the following output −

Given List is: [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
Least frequent element in the input list is:
10


## Method 3: Using Counter() Function

Counter() function(returns the frequency of words as a key-value pair)

### Example

The following program returns the least frequent element in an input array/list using the Counter() function −

# importing Counter from the collections module
from collections import Counter
# creating a function for returning the least frequent element
# by accepting the input list and input list length as arguments
def leastFrequencyElement(inputList, listLength):
# getting the frequency of all elements of a list
hashTable = Counter(inputList)
# Setting the minimum frequency(minimumCount) as length of list + 1
minimumCount = listLength + 1
# Variable to store the resultant least frequent element
resultElement = -1
# iterating the hash table
for k in hashTable:
# Check if the minimum count is greater or equal to the frequency of the key
if (minimumCount >= hashTable[k]):
# If it is true then this key will be the current least frequent element
resultElement = k
# Set the minimum count as the current key frequency value
minimumCount = hashTable[k]
# returning the least frequent element
return resultElement
# input list
inputList = [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
# getting list length
listLength = len(inputList)
print("The Given List is:", inputList)
print("Least frequent element in the input list is:")
# calling the leastFrequencyElement function by passing
# input list and list length as arguments
print(leastFrequencyElement(inputList, listLength))


### Output

On execution, the above program will generate the following output −

The Given List is: [3, 10, 3, 1, 5, 5, 4, 4, 2, 2, 2, 1, 1]
Least frequent element in the input list is:
10


## Conclusion

We learned how to find the least frequent element in a given list using three distinct ways in this article. We also learned how to perform hashing in Python, which we can use to get all the unique elements, search in O(1) time, and so on. We learned how to perform hashing using the Counter() function.

Updated on: 23-Jan-2023

492 Views 