# Mode of frequencies of given array elements

• Difficulty Level : Medium
• Last Updated : 27 Aug, 2021

Given an array arr[], the task is to find the mode of frequencies of elements of the given array.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 ``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);``    ``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 mp1 = ``new` `HashMap();` `    ``// 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 mp2 = ``new` `HashMap();` `    ``for``(Map.Entry 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 it : mp2.entrySet())``    ``{``        ``M = Math.max(M, it.getValue());``    ``}` `    ``// Search for this Mode``    ``for``(Map.Entry 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

 ``
Output:
`2`

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

My Personal Notes arrow_drop_up