Find element with highest frequency in given nested Array
Last Updated :
28 Jun, 2021
Given an array arr[] of N integers. The task is to create a frequency array freq[] of the given array arr[] and find the maximum element of the frequency array. If two elements have the same frequency in the array freq[], then return the element which has a smaller value.
Examples:
Input: arr[] = {1, 1, 1, 2, 3, 2, 2, 3, 5, 5, 5, 5, 4, 4, 4, 4, 4};
Output: 3
Explanation:
frequency of elements is given by:
val -> freq[]
1 -> 3
2 -> 3
3 -> 2
4 -> 5
5 -> 4
Element 3 has the maximum frequency in the frequency array.
Input: arr[] = { 3, 5, 15, 51, 15, 14, 14, 14, 14, 4};
Output: 1
Explanation:
frequency of elements is given by:
val -> freq[]
3 -> 1
4 -> 1
5 -> 1
14 -> 4
15 -> 2
51 -> 1
Element 1 has the maximum frequency in the frequency array.
Approach:
- Store the frequency of the elements of arr[] in a map say map1, with elements of arr[] as key and their frequency as value.
- Now, store the frequency of elements of map1 in some other map say map2.
- Traverse map2 to get the highest element.
- If there is multiple highest element than the element which has lower value is print.
Below is the implementation of the above approach:
C++14
// C++14 program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to get the highest
// frequency of frequency array
int findElement(int a[], int n)
{
// To find the maximum frequency
// initialize it with INT_MIN
int mx = INT_MIN;
int ans = 0;
// Initialize maps to store the
// count of element in array
map<int, int> map1, map2;
// Store the frequency of
// element of array in map1
for (int i = 0; i < n; i++) {
map1[a[i]]++;
}
// Storing the frequency i.e.,
// (x.second) which is count of
// element in array
for (auto x : map1) {
map2[x.second]++;
}
for (auto x : map2) {
// Check if the frequency of
// element is greater than mx
if (x.second > mx) {
mx = x.second;
// Store the value to check
// when frequency is same
ans = x.first;
}
// If frequency of 2 element is
// same than storing minimum value
else if (x.second == mx) {
ans = min(ans, x.first);
}
}
// Return the highest frequency
return ans;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 1, 1, 2, 3, 2, 2, 3, 5,
5, 5, 5, 4, 4, 4, 4, 4 };
// Size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << findElement(arr, n) << endl;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to get the highest
// frequency of frequency array
static int findElement(int a[], int n)
{
// To find the maximum frequency
// initialize it with INT_MIN
int mx = Integer.MIN_VALUE;
int ans = 0;
// Initialize maps to store the
// count of element in array
Map<Integer, Integer> map1 = new HashMap<>(),
map2 = new HashMap<>();
// Store the frequency of
// element of array in map1
for(int i = 0; i < n; i++)
{
map1.put(a[i], map1.getOrDefault(a[i], 0) + 1);
}
// Storing the frequency i.e.,
// (x.second) which is count of
// element in array
for(Integer x : map1.values())
{
map2.put(x, map2.getOrDefault(x, 0) + 1);
}
for(Map.Entry<Integer, Integer> x : map2.entrySet())
{
// Check if the frequency of
// element is greater than mx
if (x.getValue() > mx)
{
mx = x.getValue();
// Store the value to check
// when frequency is same
ans = x.getKey();
}
// If frequency of 2 element is
// same than storing minimum value
else if (x.getValue() == mx)
{
ans = Math.min(ans, x.getKey());
}
}
// Return the highest frequency
return ans;
}
// Driver code
public static void main (String[] args)
{
// Given array arr[]
int arr[] = { 1, 1, 1, 2, 3, 2, 2, 3, 5,
5, 5, 5, 4, 4, 4, 4, 4 };
// Size of the array
int n = arr.length;
// Function call
System.out.println(findElement(arr, n));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
import sys
# Function to get the highest
# frequency of frequency array
def findElement(a, n):
# To find the maximum frequency
# initialize it with INT_MIN
mx = -sys.maxsize - 1
ans = 0
# Initialize maps to store the
# count of element in array
map1 = {}
map2 = {}
# Store the frequency of
# element of array in map1
for i in a:
map1[i] = map1.get(i, 0) + 1
# Storing the frequency i.e.,
# (x.second) which is count of
# element in array
for x in map1:
map2[map1[x]] = map2.get(map1[x], 0) + 1
for x in map2:
# Check if the frequency of
# element is greater than mx
if (map2[x] > mx):
mx = map2[x]
# Store the value to check
# when frequency is same
ans = x
# If frequency of 2 element is
# same than storing minimum value
elif (map2[x] == mx):
ans = min(ans, x)
# Return the highest frequency
return ans
# Driver Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 1, 1, 1, 2, 3, 2, 2, 3,
5, 5, 5, 5, 4, 4, 4, 4, 4]
# Size of the array
n = len(arr)
# Function call
print(findElement(arr, n))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to get the highest
// frequency of frequency array
static int findElement(int[] a, int n)
{
// To find the maximum frequency
// initialize it with INT_MIN
int mx = Int32.MinValue;
int ans = 0;
// Initialize maps to store the
// count of element in array
Dictionary<int,
int> map1 = new Dictionary<int,
int>(),
map2 = new Dictionary<int,
int>();
// Store the frequency of
// element of array in map1
for(int i = 0; i < n; i++)
{
if (map1.ContainsKey(a[i]))
map1[a[i]] = map1[a[i]] + 1;
else
map1[a[i]] = 1;
}
// Storing the frequency i.e.,
// (x.second) which is count of
// element in array
foreach(KeyValuePair<int, int> xx in map1)
{
int x = xx.Value;
if (map2.ContainsKey(x))
map2[x] = map2[x] + 1;
else
map2[x] = 1;
}
foreach(KeyValuePair<int, int> x in map2)
{
// Check if the frequency of
// element is greater than mx
if (x.Value > mx)
{
mx = x.Value;
// Store the value to check
// when frequency is same
ans = x.Key;
}
// If frequency of 2 element is
// same than storing minimum value
else if (x.Value == mx)
{
ans = Math.Min(ans, x.Key);
}
}
// Return the highest frequency
return ans;
}
// Driver code
static public void Main ()
{
// Given array arr[]
int[] arr = { 1, 1, 1, 2, 3, 2, 2, 3, 5,
5, 5, 5, 4, 4, 4, 4, 4 };
// Size of the array
int n = arr.Length;
// Function call
Console.WriteLine(findElement(arr, n));
}
}
// This code is contributed by offbeat
JavaScript
<script>
// Javascript program for the above approach
// Function to get the highest
// frequency of frequency array
function findElement(a, n)
{
// To find the maximum frequency
// initialize it with INT_MIN
var mx = -1000000000;
var ans = 0;
// Initialize maps to store the
// count of element in array
var map1 = new Map(), map2= new Map();
// Store the frequency of
// element of array in map1
for (var i = 0; i < n; i++) {
if(map1.has(a[i]))
map1.set(a[i], map1.get(a[i])+1)
else
map1.set(a[i], 1);
}
// Storing the frequency i.e.,
// (x.second) which is count of
// element in array
map1.forEach((value, key) => {
if(map2.has(value))
map2.set(value, map2.get(value)+1)
else
map2.set(value, 1);
});
map2.forEach((value, key) => {
// Check if the frequency of
// element is greater than mx
if (value > mx) {
mx = value;
// Store the value to check
// when frequency is same
ans = key;
}
// If frequency of 2 element is
// same than storing minimum value
else if (value == mx) {
ans = Math.min(ans, key);
}
});
// Return the highest frequency
return ans;
}
// Driver Code
// Given array arr[]
var arr = [1, 1, 1, 2, 3, 2, 2, 3, 5,
5, 5, 5, 4, 4, 4, 4, 4];
// Size of the array
var n = arr.length;
// Function Call
document.write( findElement(arr, n));
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Find the frequency of each element in a sorted array Given a sorted array, arr[] consisting of N integers, the task is to find the frequencies of each array element. Examples: Input: arr[] = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10} Output: Frequency of 1 is: 3 Frequency of 2 is: 1 Frequency of 3 is: 2 Frequency of 5 is: 2 Frequency of 8 is: 3 Frequ
10 min read
Element with Largest Frequency in List We are given a list we need to find the element with largest frequency in a list . For example, we are having a list a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to find the element with most frequency which will be 4 in this case.Using collections.CounterCounter is a convenient way to count elements
3 min read
Find highest frequency of non-negative powers that are same as indices of elements in given Array Given an array arr[] with N non-negative integers, find the maximum number of elements that are the same non-negative powers of their indices. arr[i] = iX, where X is a non negative number. The task is to return the maximum frequency of X. Example: Input: arr = [1, 1, 4, 17]Output: 2Explanation: The
12 min read
Find maximum element among the elements with minimum frequency in given Array Given an array arr[] consisting of N integers, the task is to find the maximum element with the minimum frequency. Examples: Input: arr[] = {2, 2, 5, 50, 1}Output: 50Explanation:The element with minimum frequency is {1, 5, 50}. The maximum element among these element is 50. Input: arr[] = {3, 2, 5,
13 min read
Find given occurrences of Mth most frequent element of Array Given an array arr[], integer M and an array query[] containing Q queries, the task is to find the query[i]th occurrence of Mth most frequent element of the array. Examples: Input: arr[] = {1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2}, M = 1, query[] = {100, 4, 2}Output: -1, 12, 5Explanation: Here most f
9 min read
Find frequency of each element in given Matrix Given a matrix arr[] of size N*N containing English alphabets characters, the task is to find the frequency of all the matrix elements. Note: Print them in the decreasing order of the frequency. Examples: Input: N = 2, arr[] = {{'a', 'b'}, {'a', 'c'}}Output: a : 2, b : 1, c : 1Explanation: The frequ
6 min read
Find the most frequent element K positions apart from X in given Array Given an array nums[], and integer K and X, the task is to find the most frequent element K positions away from X in the given array. Examples: Input: nums = [1, 100, 200, 1, 100], K = 1, X = 1Output: 100Explanation: Elements 1 position apart from 1 is only 100.So the answer is 100. Input: nums = [2
6 min read
Maximum element in an array which is equal to its frequency Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it's value Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 3 Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 are elements which h
11 min read
Find elements having at least K times the minimum frequency Given an array A, the task is to find elements with the occurrence at least K times the frequency of the least occurring element in the array. Examples: Input: A = {4, 4, 3, 6, 6, 6, 8, 9}, K = 2Output: {4, 6}Explanation: Frequency Table : {4:2, 3:1, 6:3, 8:1, 9:1}, least occurring elements in nums
6 min read
Find the array element having maximum frequency of the digit K Given an array arr[] of size N and an integer K, the task is to find an array element that contains the digit K a maximum number of times. If more than one solutions exist, then print any one of them. Otherwise, print -1. Examples: Input: arr[] = {3, 77, 343, 456}, K = 3 Output: 343 Explanation: Fre
15 min read