Given an array of integers, find the most occurring element of the array and return any one of its indexes randomly with equal probability.
Examples:
Input: arr[] = [-1, 4, 9, 7, 7, 2, 7, 3, 0, 9, 6, 5, 7, 8, 9] Output: Element with maximum frequency present at index 6 OR Element with maximum frequency present at Index 3 OR Element with maximum frequency present at index 4 OR Element with maximum frequency present at index 12 All outputs above have equal probability.
The idea is to iterate through the array once and find out the maximum occurring element and its frequency n. Then we generate a random number r between 1 and n and finally return the r’th occurrence of maximum occurring element in the array.
Below is C++ implementation of above idea –
// C++ program to return index of most occurring element // of the array randomly with equal probability #include <iostream> #include <unordered_map> #include <climits> using namespace std; // Function to return index of most occurring element // of the array randomly with equal probability void findRandomIndexOfMax(int arr[], int n) { // freq store frequency of each element in the array unordered_map<int, int> freq; for (int i = 0; i < n; i++) freq[arr[i]] += 1; int max_element; // stores max occurring element // stores count of max occurring element int max_so_far = INT_MIN; // traverse each pair in map and find maximum // occurring element and its frequency for (pair<int, int> p : freq) { if (p.second > max_so_far) { max_so_far = p.second; max_element = p.first; } } // generate a random number between [1, max_so_far] int r = (rand() % max_so_far) + 1; // traverse array again and return index of rth // occurrence of max element for (int i = 0, count = 0; i < n; i++) { if (arr[i] == max_element) count++; // print index of rth occurence of max element if (count == r) { cout << "Element with maximum frequency present " "at index " << i << endl; break; } } } // Driver code int main() { // input array int arr[] = { -1, 4, 9, 7, 7, 2, 7, 3, 0, 9, 6, 5, 7, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); // randomize seed srand(time(NULL)); findRandomIndexOfMax(arr, n); return 0; } |
Output:
Element with maximum frequency present at index 12
Time complexity of above solution is O(n).
Auxiliary space used by the program is O(n).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Generate integer from 1 to 7 with equal probability
- Select a Random Node from a tree with equal probability
- Generate 0 and 1 with 25% and 75% probability
- Random number generator in arbitrary probability distribution fashion
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- Generating Random String Using PHP
- Generating OTP (One time Password) in PHP
- Shuffle or Randomize a list in Java
- Mid-Square hashing
- Strong Password Suggester Program
- Implement random-0-6-Generator using the given random-0-1-Generator
- Select a Random Node from a tree with equal probability
- QuickSort using Random Pivoting
- Operations on Sparse Matrices



