Mode of frequencies of given array elements
Given an array arr[], the task is to find the mode of frequencies of elements of the given array.
Examples:
Input: arr[] = {6, 10, 3, 10, 8, 3, 6, 4}, N = 8
Output: 2
Explanation:
Here (3, 10 and 6) have frequency 2, while (4 and 8) have frequency 1.
Three numbers have frequency 2, while 2 numbers have frequency 1.
Thus, the mode of the frequencies is 2.Input: arr[] = {5, 9, 2, 9, 7, 2, 5, 3, 1}, N = 9
Output:1
Approach: The idea is to find the frequency of the frequencies of all array elements. Finally, compute the mode of the frequencies.
Below is the implementation of the above approach:
C++
// C++ program of the // above approach #include <bits/stdc++.h> using namespace std; // Function to find the mode of the // frequency of the array int countFreq( int arr[], int n) { // Stores the frequencies // of array elements unordered_map< int , int > mp1; // Traverse through array // elements and // count frequencies for ( int i = 0; i < n; ++i) { mp1[arr[i]]++; } // Stores the frequencies's of // frequencies of array elements unordered_map< int , int > mp2; for ( auto it : mp1) { mp2[it.second]++; } // Stores the minimum value int M = INT_MIN; // Find the Mode in 2nd map for ( auto it : mp2) { M = max(M, it.second); } // search for this Mode for ( auto it : mp2) { // When mode is find then return // to main function. if (M == it.second) { return it.first; } } // If mode is not found return 0; } // Driver Code int main() { int arr[] = { 6, 10, 3, 10, 8, 3, 6, 4 }; int n = sizeof (arr) / sizeof (arr[0]); cout << countFreq(arr, n); return 0; } |
Java
// Java program of the // above approach import java.util.*; class GFG{ // Function to find the mode of the // frequency of the array static int countFreq( int arr[], int n) { // Stores the frequencies // of array elements HashMap<Integer, Integer> mp1 = new HashMap<Integer, Integer>(); // Traverse through array // elements and // count frequencies for ( int i = 0 ; i < n; ++i) { if (mp1.containsKey(arr[i])) { mp1.put(arr[i], mp1.get(arr[i]) + 1 ); } else { mp1.put(arr[i], 1 ); } } // Stores the frequencies's of // frequencies of array elements HashMap<Integer, Integer> mp2 = new HashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> it : mp1.entrySet()) { if (mp2.containsKey(it.getValue())) { mp2.put(it.getValue(), mp2.get(it.getValue()) + 1 ); } else { mp2.put(it.getValue(), 1 ); } } // Stores the minimum value int M = Integer.MIN_VALUE; // Find the Mode in 2nd map for (Map.Entry<Integer, Integer> it : mp2.entrySet()) { M = Math.max(M, it.getValue()); } // Search for this Mode for (Map.Entry<Integer, Integer> it : mp2.entrySet()) { // When mode is find then return // to main function. if (M == it.getValue()) { return it.getKey(); } } // If mode is not found return 0 ; } // Driver Code public static void main(String[] args) { int arr[] = { 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 }; int n = arr.length; System.out.print(countFreq(arr, n)); } } // This code is contributed by Amit Katiyar |
Python3
# Python3 program of the # above approach from collections import defaultdict import sys # Function to find the mode of the # frequency of the array def countFreq(arr, n): # Stores the frequencies # of array elements mp1 = defaultdict ( int ) # Traverse through array # elements and # count frequencies for i in range (n): mp1[arr[i]] + = 1 # Stores the frequencies's of # frequencies of array elements mp2 = defaultdict ( int ) for it in mp1: mp2[mp1[it]] + = 1 # Stores the minimum value M = - sys.maxsize - 1 # Find the Mode in 2nd map for it in mp2: M = max (M, mp2[it]) # search for this Mode for it in mp2: # When mode is find then return # to main function. if (M = = mp2[it]): return it # If mode is not found return 0 # Driver Code if __name__ = = "__main__" : arr = [ 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 ] n = len (arr) print (countFreq(arr, n)) # This code is contributed by Chitranayal |
C#
// C# program of the // above approach using System; using System.Collections.Generic; class GFG{ // Function to find the mode of the // frequency of the array static int countFreq( int []arr, int n) { // Stores the frequencies // of array elements Dictionary< int , int > mp1 = new Dictionary< int , int >(); // Traverse through array // elements and // count frequencies for ( int i = 0; i < n; ++i) { if (mp1.ContainsKey(arr[i])) { mp1[arr[i]] = mp1[arr[i]] + 1; } else { mp1.Add(arr[i], 1); } } // Stores the frequencies's of // frequencies of array elements Dictionary< int , int > mp2 = new Dictionary< int , int >(); foreach (KeyValuePair< int , int > it in mp1) { if (mp2.ContainsKey(it.Value)) { mp2[it.Value] = mp2[it.Value] + 1; } else { mp2.Add(it.Value, 1); } } // Stores the minimum value int M = int .MinValue; // Find the Mode in 2nd map foreach (KeyValuePair< int , int > it in mp2) { M = Math.Max(M, it.Value); } // Search for this Mode foreach (KeyValuePair< int , int > it in mp2) { // When mode is find then return // to main function. if (M == it.Value) { return it.Key; } } // If mode is not found return 0; } // Driver Code public static void Main(String[] args) { int []arr = { 6, 10, 3, 10, 8, 3, 6, 4 }; int n = arr.Length; Console.Write(countFreq(arr, n)); } } // This code is contributed by Amit Katiyar |
Javascript
<script> // Javascript program of the // above approach // Function to find the mode of the // frequency of the array function countFreq(arr,n) { // Stores the frequencies // of array elements let mp1 = new Map(); // Traverse through array // elements and // count frequencies for (let i = 0; i < n; ++i) { if (mp1.has(arr[i])) { mp1.set(arr[i], mp1.get(arr[i]) + 1); } else { mp1.set(arr[i], 1); } } // Stores the frequencies's of // frequencies of array elements let mp2 = new Map(); for (let [key, value] of mp1.entries()) { if (mp2.has(value)) { mp2.set(value, mp2.get(value) + 1); } else { mp2.set(value, 1); } } // Stores the minimum value let M = Number.MIN_VALUE; // Find the Mode in 2nd map for (let [key, value] of mp2.entries()) { M = Math.max(M, value); } // Search for this Mode for (let [key, value] of mp2.entries()) { // When mode is find then return // to main function. if (M == value) { return key; } } // If mode is not found return 0; } // Driver Code let arr = [ 6, 10, 3, 10, 8, 3, 6, 4]; let n = arr.length; document.write(countFreq(arr, n)); // This code is contributed by avanitrachhadiya2155 </script> |
Output:
2
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...