Sunday, November 17, 2024
Google search engine
HomeLanguagesPython Program for Least frequent element in an array

Python Program for Least frequent element in an array

Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them.
Examples : 
 

Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1}
Output : 3
3 appears minimum number of times in given
array.

Input : arr[] = {10, 20, 30}
Output : 10 or 20 or 30

 

Method 1:

A simple solution is to run two loops. The outer loop picks all elements one by one. The inner loop finds frequency of the picked element and compares with the minimum so far. Time complexity of this solution is O(n2)
A better solution is to do sorting. We first sort the array, then linearly traverse the array.
 

Python3




# Python 3 program to find the least
# frequent element in an array.
 
 
def leastFrequent(arr, n):
 
    # Sort the array
    arr.sort()
 
    # find the min frequency using
    # linear traversal
    min_count = n + 1
    res = -1
    curr_count = 1
    for i in range(1, n):
        if (arr[i] == arr[i - 1]):
            curr_count = curr_count + 1
        else:
            if (curr_count < min_count):
                min_count = curr_count
                res = arr[i - 1]
 
            curr_count = 1
 
    # If last element is least frequent
    if (curr_count < min_count):
        min_count = curr_count
        res = arr[n - 1]
 
    return res
 
 
# Driver program
arr = [1, 3, 2, 1, 2, 2, 3, 1]
n = len(arr)
print(leastFrequent(arr, n))


Output

3

Time Complexity : O(n Log n) 
Auxiliary Space : O(1)

Method 2:

An efficient solution is to use hashing. We create a hash table and store elements and their frequency counts as key value pairs. Finally we traverse the hash table and print the key with minimum value.
 

Python3




# Python3 program to find the most
# frequent element in an array.
import math as mt
 
def leastFrequent(arr, n):
 
    # Insert all elements in Hash.
    Hash = dict()
    for i in range(n):
        if arr[i] in Hash.keys():
            Hash[arr[i]] += 1
        else:
            Hash[arr[i]] = 1
 
    # find the max frequency
    min_count = n + 1
    res = -1
    for i in Hash:
        if (min_count >= Hash[i]):
            res = i
            min_count = Hash[i]
         
    return res
 
# Driver Code
arr = [1, 3, 2, 1, 2, 2, 3, 1]
n = len(arr)
print(leastFrequent(arr, n))


Output

3

Time Complexity : O(n) 
Auxiliary Space : O(n)
 

Method 3: Using Counter() Function

Python3




# Python3 program to find the most
# frequent element in an array.
import math as mt
from collections import Counter
 
 
def leastFrequent(arr, n):
 
    # Getting frequency of all elements
    Hash = Counter(arr)
 
    # find the max frequency
    min_count = n + 1
    res = -1
    for i in Hash:
        if (min_count >= Hash[i]):
            res = i
            min_count = Hash[i]
 
    return res
 
 
# Driver Code
arr = [1, 3, 2, 1, 2, 2, 3, 1]
n = len(arr)
print(leastFrequent(arr, n))


Output

3

Please refer complete article on Least frequent element in an array for more details!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments