Find given occurrences of Mth most frequent element of Array
Last Updated :
20 Feb, 2023
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, 5
Explanation: Here most frequent Integer = 8, with frequency = 4
Thus for Query
1) For k = 100, Output-> -1 (100th element not available)
2) For k = 4, Output -> 12 (4th occurrence of 8 is at 12th index)
3) For k = 2, Output -> 5 (2nd occurrence of 8 is at 5th index)
Input: arr[] = {2, 2, 20, 8, 8, 1, 2, 5}, M = 2, query[] = {2, 3}
Output: 4, -1
Approach: The solution to the problem is based on the following idea:
Find the Mth most frequent element. Then store all the occurrences of the integers and find the query[i]th occurrence of the Mth most frequent element.
Follow the steps mentioned below to implement the idea:
- Declare map for integer and vector.
- Traverse through the input array and store the unique elements in the map as keys.
- Then push the index of the occurrences of that element into the vector.
- Store the unique elements with their frequency and find the Mth most frequent element.
- Again traverse through the queries.
- For each query, check if K < vector size then return -1.
- Else store the indexes of the element.
Follow the below illustration for a better understanding
Illustration:
arr[] = {1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2}, M = 1
Query_val = {100, 4, 2}
Now for Each element the frequency and occurred indexes are:
1 -> 1, 6 frequency : 2
2 -> 2, 7, 13 frequency : 3
20 -> 3 frequency : 1
8 -> 4, 5, 9, 12 frequency : 4
5 -> 8 frequency : 1
0 -> 10 frequency : 1
6 -> 11 frequency : 1
Thus maximum frequency element = 8 with frequency 4
Now for queries
For k = 100, -> -1 (k > maxfre)
For K = 4, -> 12 ((K-1) index of vector)
For K = 2, -> 5 ((K-1) index of vector)
Below is the implementation of the above approach:
C++
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Function for finding indexes
vector<int> findindexes(vector<int> arr, int M,
vector<int> query)
{
// Unordered map to store the unique
// element as keys and vector as value
// to store indexes
unordered_map<int, vector<int> > fre;
int n = arr.size();
// Ans vector to store and return
// indexes occurred
vector<int> ans;
for (int i = 0; i < n; i++) {
// For each key push that particular
// index+1 1-bases indexing
fre[arr[i]].push_back(i + 1);
}
set<pair<int, int> > st;
for (auto it = fre.begin();
it != fre.end(); it++) {
st.insert({ it->second.size(),
it->first });
}
int maxelement, i = 0;
for (auto it = st.rbegin(); it != st.rend()
and i < M;
it--, i++) {
maxelement = (*it).second;
}
for (int i = 0; i < query.size(); i++) {
// If kth index is not available
if (fre[maxelement].size() < query[i])
ans.push_back(-1);
else {
// Storing the indexes
// of maxfre element
ans.push_back(
fre[maxelement][query[i] - 1]);
}
}
return ans;
}
// Driver code
int main()
{
// Input array
vector<int> arr
= { 1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2 };
int M = 1;
vector<int> query = { 100, 4, 2 };
// Function call
vector<int> ans = findindexes(arr, M, query);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return 0;
}
Java
import java.util.*;
import java.io.*;
// Java program for the above approach
class GFG{
// Function for finding indexes
public static ArrayList<Integer> findindexes(ArrayList<Integer> arr, int M, ArrayList<Integer> query)
{
// Unordered map to store the unique
// element as keys and vector as value
// to store indexes
HashMap<Integer, ArrayList<Integer>> fre = new HashMap<Integer, ArrayList<Integer>>();
int n = arr.size();
// Ans vector to store and return
// indexes occurred
ArrayList<Integer> ans = new ArrayList<Integer>();
int i;
for (i = 0 ; i < n ; i++) {
// For each key push that particular
// index+1 1-bases indexing
if(!fre.containsKey(arr.get(i))){
fre.put(arr.get(i), new ArrayList<Integer>());
}
fre.get(arr.get(i)).add(i + 1);
}
TreeSet<pair> st = new TreeSet<pair>(new Comp());
for (Map.Entry<Integer, ArrayList<Integer>> it : fre.entrySet()){
st.add(new pair(it.getValue().size(), it.getKey()));
}
int maxelement = 0;
i = 0;
for (pair it : st) {
if(i >= M) break;
maxelement = it.second;
i++;
}
for (i = 0 ; i < query.size() ; i++) {
// If kth index is not available
if(!fre.containsKey(maxelement))
{
ans.add(-1);
}
else
{
if (fre.get(maxelement).size() < query.get(i))
ans.add(-1);
else {
// Storing the indexes
// of maxfre element
ans.add(fre.get(maxelement).get(query.get(i) - 1));
}
}
}
return ans;
}
// Driver Code
public static void main(String args[])
{
// Input array
ArrayList<Integer> arr = new ArrayList<Integer>(
List.of(1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2)
);
int M = 1;
ArrayList<Integer> query = new ArrayList<Integer>(
List.of(100, 4, 2)
);
// Function call
ArrayList<Integer> ans = findindexes(arr, M, query);
for (int i = 0 ; i < ans.size() ; i++) {
System.out.print(ans.get(i) + " ");
}
}
}
public class pair{
public int first;
public int second;
public pair(int first,int second){
this.first = first;
this.second = second;
}
}
class Comp implements Comparator<pair>{
public int compare(pair o2, pair o1){
if(o1.first == o2.first){
return o1.second - o2.second;
}
return o1.first - o2.first;
}
}
// This code is contributed by entertin2022.
Python3
# Python3 code to implement the above approach
# Function for finding indexes
def findindexes(arr, M, query):
# Unordered map to store the unique
# element as keys and vector as value
# to store indexes
fre = dict()
n = len(arr)
# Ans vector to store and return
# indexes occurred
ans = []
for i in range(n):
# For each key push that particular
# index+1 1-bases indexing
if arr[i] not in fre:
fre[arr[i]] = []
fre[arr[i]].append(i + 1)
st = set()
for it in fre:
st.add((len(fre[it]), it))
# sorting the st in reverse
st = sorted(st, reverse=True)
i = 0
for it in st:
if i >= M:
break
maxelement = it[1]
i += 1
for i in range(len(query)):
# If kth index is not available
if len(fre[maxelement]) < query[i]:
ans.append(-1)
else:
# Storing the indexes
# of maxfre element
ans.append(fre[maxelement][query[i] - 1])
return ans
# Driver code
# Input array
arr = [1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2]
M = 1
query = [100, 4, 2]
# Function call
ans = findindexes(arr, M, query)
print(" ".join(map(str, ans)))
# This code is contributed by phasing17
C#
// C# program to implement above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function for finding indexes
public static List<int> findindexes(List<int> arr, int M, List<int> query)
{
// Unordered map to store the unique
// element as keys and vector as value
// to store indexes
Dictionary<int, List<int>> fre = new Dictionary<int, List<int>>();
int n = arr.Count;
// Ans vector to store and return
// indexes occurred
List<int> ans = new List<int>();
int i;
for (i = 0 ; i < n ; i++) {
// For each key push that particular
// index+1 1-bases indexing
if(!fre.ContainsKey(arr[i])){
fre.Add(arr[i], new List<int>());
}
fre[arr[i]].Add(i + 1);
}
SortedSet<pair> st = new SortedSet<pair>(new Comp());
foreach(KeyValuePair<int, List<int>> it in fre){
st.Add(new pair(it.Value.Count, it.Key));
}
int maxelement = 0;
i = 0;
foreach (pair it in st) {
if(i >= M) break;
maxelement = it.second;
i++;
}
for (i = 0 ; i < query.Count ; i++) {
// If kth index is not available
if(!fre.ContainsKey(maxelement))
{
ans.Add(-1);
}
else
{
if (fre[maxelement].Count < query[i])
ans.Add(-1);
else {
// Storing the indexes
// of maxfre element
ans.Add(fre[maxelement][query[i] - 1]);
}
}
}
return ans;
}
// Driver Code
public static void Main(string[] args){
// Input array
List<int> arr = new List<int>{
1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2
};
int M = 1;
List<int> query = new List<int>{
100, 4, 2
};
// Function call
List<int> ans = findindexes(arr, M, query);
for (int i = 0 ; i < ans.Count ; i++) {
Console.Write(ans[i] + " ");
}
}
}
public class pair{
public int first{ get; set; }
public int second{ get; set; }
public pair(int first,int second){
this.first = first;
this.second = second;
}
}
class Comp : IComparer<pair>{
public int Compare(pair o2,pair o1){
if(o1.first == o2.first){
return o1.second - o2.second;
}
return o1.first - o2.first;
}
}
JavaScript
//JavaScript code to implement the above approach
// Function for finding indexes
function findindexes(arr, M, query)
{
// Unordered map to store the unique
// element as keys and vector as value
// to store indexes
let fre = {};
let n = arr.length;
// Ans vector to store and return
// indexes occurred
let ans = [];
for (var i = 0; i < n; i++)
{
// For each key push that particular
// index+1 1-bases indexing
if (!fre.hasOwnProperty(arr[i]))
fre[arr[i]] = [];
fre[arr[i]].push(i + 1);
}
let st = [];
for (const [it, val] of Object.entries(fre))
{
st.push([fre[it].length, parseInt(it)]);
}
// sorting the st in reverse
st.sort();
st.reverse();
i = 0;
let maxelement;
for (var it of st)
{
if (i >= M)
break;
maxelement = it[1];
i += 1;
}
for (var i = 0; i < query.length; i++)
{
// If kth index is not available
if (fre[maxelement].length < query[i])
ans.push(-1);
else
{
// Storing the indexes
// of maxfre element
ans.push(fre[maxelement][query[i] - 1]);
}
}
return ans;
}
// Driver code
// Input array
let arr = [1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2];
let M = 1;
let query = [100, 4, 2];
// Function call
let ans = findindexes(arr, M, query);
console.log(ans.join(" "));
// This code is contributed by phasing17
Time Complexity: O(N + Q + K * logK) Q = Number of queries, K = number of unique elements.
Auxiliary Space: O(N) where N is extra space as we are using unordered map to store element.
Similar Reads
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 = 8Output: 2Explanation:Here (3, 10 and 6) have frequency 2, while (4 and 8) have frequency 1.Three numbers have frequency 2, while 2 numbers have
6 min read
Remove an occurrence of most frequent array element exactly K times
Given an array arr[], the task is to remove an occurrence of the most frequent array element exactly K times. If multiple array elements have maximum frequency, remove the smallest of them. Print the K deleted elements. Examples: Input: arr[] = {1, 3, 2, 1, 4, 1}, K = 2Output: 1 1Explanation: The fr
12 min read
Find GCD of most occurring and least occurring elements of given Array
Given an array arr[] of size n, The task is to find the GCD of the highest and lowest frequency element in the given array. Examples: Input: arr[] = {2, 2, 4, 4, 5, 5, 6, 6, 6, 6}Output: 2Explanation: The frequency of the elements in the above array is freq(2) = 2, freq(4) = 2, freq(5) = 2, freq(6)
7 min read
Find element with highest frequency in given nested Array
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
8 min read
Find the Kth occurrence of an element in a sorted Array
Given a sorted array arr[] of size N, an integer X, and a positive integer K, the task is to find the index of Kth occurrence of X in the given array. Examples: Input: N = 10, arr[] = [1, 2, 3, 3, 4, 5, 5, 5, 5, 5], X = 5, K = 2Output: Starting index of the array is '0' Second occurrence of 5 is at
15+ 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
Find frequency of each element in given 3D Array
Given a 3D array of size N*M*P consisting only of English alphabet characters, the task is to print the frequency of all the elements in increasing order. If the frequency is the same, print them in lexicographic ordering. Examples: Input: N = 3, M = 4, P = 5, matrix = { { {a, b, c, d, e}, {f, g, h,
9 min read
Elements of first array that have more frequencies
Given two arrays (which may or may not be sorted). These arrays are such that they might have some common elements in them. We need to find elements whose counts of occurrences are more in first array than second. Examples: Input : ar1[] = {1, 2, 2, 2, 3, 3, 4, 5} ar2[] = {2, 2, 3, 3, 3, 4} Output :
7 min read
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
Find array elements with frequencies in range [l , r]
Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
9 min read