Find frequency of each element in a limited range array in less than O(n) time - GeeksforGeeks

15 Feb, 2022
Given a sorted array of positive integers, the count number of occurrences for each element in the array. Assume all elements in the array are less than some constant M.Do this without traversing the complete array. i.e. expected time complexity is less than O(n). 
Examples: 
Input: arr[] = [1, 1, 1, 2, 3, 3, 5,
               5, 8, 8, 8, 9, 9, 10] 
Output:
Element 1 occurs 3 times
Element 2 occurs 1 times
Element 3 occurs 2 times
Element 5 occurs 2 times
Element 8 occurs 3 times
Element 9 occurs 2 times
Element 10 occurs 1 times

Input: arr[] = [2, 2, 6, 6, 7, 7, 7, 11] 
Output:
Element 2 occurs 2 times
Element 6 occurs 2 times
Element 7 occurs 3 times
Element 11 occurs 1 times
Method 1: This method uses the technique of Linear Search without using auxiliary space.
Approach:  The idea is to traverse the input array and increment the frequency of the element if the current element and the previous element are same, otherwise reset the frequency and print the element and its frequency.
Algorithm: Initialise frequency to 1 and index to 1.Traverse the array from the index position and check if the current element  is equal to previous element.If yes, increment the frequency and index and repeat the step 2. Otherwise, print the element and its frequency and repeat step 2.At last(corner case), print the last element and its frequency.
Initialise frequency to 1 and index to 1.
Traverse the array from the index position and check if the current element  is equal to previous element.
If yes, increment the frequency and index and repeat the step 2. Otherwise, print the element and its frequency and repeat step 2.
At last(corner case), print the last element and its frequency.
Implementation:
C++
Java
Python3
C#
Javascript
// C++ program to count number of occurrences of// each element in the array in O(n) time and O(1) space #include <iostream>using namespace std;void findFrequencies(int ele[], int n){    int freq = 1;    int idx = 1;    int element = ele[0];    while (idx < n) {        // check if the current element is equal to        // previous element.        if (ele[idx - 1] == ele[idx]) {            freq++;            idx++;        }        else {            cout << element << " " << freq << endl;            element = ele[idx];            idx++;                       // reset the frequency            freq = 1;        }    }       // print the last element and its frequency    cout << element << " " << freq;} int main(){    cout << "---frequencies in a sorted array----" << endl;    int arr[]        = { 10, 20, 30, 30, 30, 40, 50, 50, 50, 50, 70 };    int n = sizeof(arr) / sizeof(arr[0]);    findFrequencies(arr, n);} // This code is contributed by anushkaseehh
// Java program to count number of occurrences of// each element in the array in O(n) time and O(1) space      function void findFrequencies(ele)    {        var freq = 1;        var idx = 1;        var element = ele[0];        while (idx < ele.length) {            // check if the current element is equal to            // previous element.            if (ele[idx - 1] == ele[idx]) {                freq++;                idx++;            }            else {                document.write(element + " " + freq+ "<br>");                element = ele[idx];                idx++;                // reset the frequency                freq = 1;            }        }        // print the last element and its frequency        document.write(element + " " + freq +"<br>");    }    // Driver code             document.write(            "---frequencies in a sorted array----"+"<br>");        findFrequencies(new Array (10, 20, 30, 30, 30, 40,                                    50, 50, 50, 50, 70 ));     //this code is contributed by shivanisinghss2110
# python program to count number of occurrences of# each element in the array in O(n) time and O(1) spacedef findFrequencies(ele, n):     freq = 1    idx = 1    element = ele[0]    while (idx < n):               # check if the current element is equal to        # previous element.        if (ele[idx - 1] == ele[idx]):            freq += 1            idx += 1                 else:            print(element , " " ,freq);            element = ele[idx]            idx += 1                       # reset the frequency            freq = 1             # print the last element and its frequency    print(element , " " , freq); print( "---frequencies in a sorted array----" );arr = [10, 20, 30, 30, 30, 40, 50, 50, 50, 50, 70 ];n = len(arr)findFrequencies(arr, n) # This code is contributed by shivanisinghss2110
// C# program to count number of occurrences of// each element in the array in O(n) time and O(1) spaceusing System;class GFG {     public static void findFrequencies(int[] ele)    {        int freq = 1;        int idx = 1;        int element = ele[0];        while (idx < ele.Length)        {                       // check if the current element is equal to            // previous element.            if (ele[idx - 1] == ele[idx]) {                freq++;                idx++;            }            else {                Console.WriteLine(element + " " + freq);                element = ele[idx];                idx++;                               // reset the frequency                freq = 1;            }        }               // print the last element and its frequency        Console.WriteLine(element + " " + freq);    }       // Driver code    public static void Main(String[] args)    {        Console.WriteLine(            "---frequencies in a sorted array----");        findFrequencies(new int[] { 10, 20, 30, 30, 30, 40,                                    50, 50, 50, 50, 70 });    }}
<script>// JavaScript program to count number of occurrences of// each element in the array in O(n) time and O(1) space    function void findFrequencies(ele)    {        var freq = 1;        var idx = 1;        var element = ele[0];        while (idx < ele.length)        {                     // check if the current element is equal to            // previous element.            if (ele[idx - 1] == ele[idx]) {                freq++;                idx++;            }            else            {                document.write(element + " " + freq);                element = ele[idx];                idx++;                                 // reset the frequency                freq = 1;            }        }                 // print the last element and its frequency        document.write(element + " " + freq);    }         // Driver code        document.write(            "---frequencies in a sorted array----");        findFrequencies(new var[] { 10, 20, 30, 30, 30, 40,                                    50, 50, 50, 50, 70 });     // This code is contributed by shivanisinghss2110</script>
---frequencies in a sorted array----
10 1
20 1
30 3
40 1
50 4
70 1
  
Method 2: This method uses the technique of Linear Search to solve the following. 
Approach: The idea is to traverse the input array and for each distinct element of the array, store its frequency in a HashMap, and finally print the HashMap.
Algorithm: Create a HashMap to map the frequency to element, i.e to store element-frequency pair.Traverse the array from start to end.For each element in the array update the frequency, i.e hm[array[i]]++Traverse the HashMap and print the element frequency pair.
Create a HashMap to map the frequency to element, i.e to store element-frequency pair.Traverse the array from start to end.For each element in the array update the frequency, i.e hm[array[i]]++Traverse the HashMap and print the element frequency pair.
Create a HashMap to map the frequency to element, i.e to store element-frequency pair.
Traverse the array from start to end.
For each element in the array update the frequency, i.e hm[array[i]]++
Traverse the HashMap and print the element frequency pair.
Implementation:
C++
Java
Python3
C#
Javascript
// C++ program to count number of occurrences of// each element in the array #include <iostream>#include <bits/stdc++.h>using namespace std; // It prints number of// occurrences of each element in the array.void findFrequency(int arr[], int n){    // HashMap to store frequencies    unordered_map<int, int> mp;     // traverse the array    for (int i = 0; i < n; i++) {        // update the frequency        mp[arr[i]]++;    }     // traverse the hashmap    for (auto i : mp) {        cout << "Element " << i.first << " occurs "             << i.second << " times" << endl;    }} // Driver functionint main(){    int arr[] = { 1, 1, 1, 2, 3, 3, 5, 5,                  8, 8, 8, 9, 9, 10 };    int n = sizeof(arr) / sizeof(arr[0]);     findFrequency(arr, n);     return 0;}
// Java program to count number// of occurrences of each// element in the arrayimport java.io.*;import java.util.*;class GFG{   // It prints number of  // occurrences of each  // element in the array.  static void findFrequency(int [] arr,                            int n)  {    Map<Integer, Integer> mp      = new HashMap<Integer, Integer>();     // traverse the array    for (int i = 0; i < n; i++)    {       // update the frequency      if (!mp.containsKey(arr[i]))        mp.put(arr[i],0);       mp.put(arr[i],mp.get(arr[i])+1);    }     // traverse the hashmap    for (Map.Entry<Integer, Integer> kvp : mp.entrySet())    {      System.out.println("Element " + kvp.getKey() +                         " occurs " + kvp.getValue() +                         " times");    }  }   // Driver function  public static void main (String[] args) {      int [] arr = {1, 1, 1, 2,                  3, 3, 5, 5,                  8, 8, 8, 9,                  9, 10};    int n = arr.length;    findFrequency(arr, n);  }} // This code is contributed by avanitrachhadiya2155
# Python program to count number of occurrences of# each element in the array #include <iostream> # It prints number of# occurrences of each element in the array.def findFrequency(arr, n):         # HashMap to store frequencies    mp = {}     # traverse the array    for i in range(n):         # update the frequency        if arr[i] not in mp:            mp[arr[i]] = 0        mp[arr[i]] += 1             # traverse the hashmap    for i in mp:        print("Element", i, "occurs", mp[i], "times")     # Driver functionarr = [1, 1, 1, 2, 3, 3, 5, 5,8, 8, 8, 9, 9, 10]n = len(arr) findFrequency(arr, n)     # This code is contributed by shubhamsingh10
// C# program to count number// of occurrences of each// element in the arrayusing System;using System.Collections.Generic;class GFG{ // It prints number of// occurrences of each// element in the array.static void findFrequency(int [] arr,                          int n){  // HashMap to store frequencies  Dictionary <int,              int > mp =              new Dictionary<int,                             int>();   // traverse the array  for (int i = 0; i < n; i++)  {    // update the frequency    if (!mp.ContainsKey(arr[i]))      mp[arr[i]] = 0;     mp[arr[i]]++;  }   // traverse the hashmap  foreach(KeyValuePair<int,                       int> kvp in mp)    Console.WriteLine("Element " + kvp.Key +                      " occurs " + kvp.Value +                      " times");} // Driver functionpublic static void Main(){  int [] arr = {1, 1, 1, 2,                3, 3, 5, 5,                8, 8, 8, 9,                9, 10};  int n = arr.Length;  findFrequency(arr, n);}} // This code is contributed by Chitranayal
<script> // Javascript program to count number// of occurrences of each// element in the array // It prints number of// occurrences of each// element in the array.function findFrequency(arr, n){    let mp = new Map();      // Traverse the array    for(let i = 0; i < n; i++)    {          // Update the frequency        if (!mp.has(arr[i]))            mp.set(arr[i],0);                 mp.set(arr[i], mp.get(arr[i]) + 1);    }      // Traverse the hashmap    for(let [key, value] of mp.entries())    {        document.write("Element " + key +                       " occurs " + value +                       " times<br>");    }} // Driver codelet arr = [ 1, 1, 1, 2, 3, 3, 5,            5, 8, 8, 8, 9, 9, 10 ];let n = arr.length; findFrequency(arr, n); // This code is contributed by patel2127 </script>
Element 10 occurs 1 times
Element 2 occurs 1 times
Element 9 occurs 2 times
Element 1 occurs 3 times
Element 8 occurs 3 times
Element 3 occurs 2 times
Element 5 occurs 2 times
Complexity Analysis: 
Time Complexity:O(n), only one traversal of the array is needed.
Space Complexity:O(n), to store the elements in the HashMap O(n) extra space is needed.
Method 3: This method uses the technique of Binary Search to arrive at the solution.  
Approach: The problem can be solved in less than O(n) time if all its elements are sorted, i.e. if similar elements exist in the array then the elements are in a contiguous subarray or it can be said that if ends of a subarray are same then all the elements inside the subarray are equal. So the count of that element is the size of the subarray and all the elements of that subarray need not be counted.  
Algorithm: Create a HashMap (hm) to store the frequency of elements.Create a recursive function that accepts an array and size.Check if the first element of the array is equal to the last element. If equal then all the elements are the same and update the frequency by hm[array[0]+=sizeElse divide the array into two equal halves and call the function recursively for both the halves.Traverse the hashmap and print the element frequency pair.
Create a HashMap (hm) to store the frequency of elements.Create a recursive function that accepts an array and size.Check if the first element of the array is equal to the last element. If equal then all the elements are the same and update the frequency by hm[array[0]+=sizeElse divide the array into two equal halves and call the function recursively for both the halves.Traverse the hashmap and print the element frequency pair.
Create a HashMap (hm) to store the frequency of elements.
Create a recursive function that accepts an array and size.
Check if the first element of the array is equal to the last element. If equal then all the elements are the same and update the frequency by hm[array[0]+=size
Else divide the array into two equal halves and call the function recursively for both the halves.
Traverse the hashmap and print the element frequency pair.
Implementation:  
C++
Java
Python3
C#
Javascript
// C++ program to count number of occurrences of// each element in the array in less than O(n) time#include <iostream>#include <vector>using namespace std; // A recursive function to count number of occurrences// for each element in the array without traversing// the whole arrayvoid findFrequencyUtil(int arr[], int low, int high,                       vector<int>& freq){    // If element at index low is equal to element    // at index high in the array    if (arr[low] == arr[high]) {        // increment the frequency of the element        // by count of elements between high and low        freq[arr[low]] += high - low + 1;    }    else {        // Find mid and recurse for left and right        // subarray        int mid = (low + high) / 2;        findFrequencyUtil(arr, low, mid, freq);        findFrequencyUtil(arr, mid + 1, high, freq);    }} // A wrapper over recursive function// findFrequencyUtil(). It print number of// occurrences of each element in the array.void findFrequency(int arr[], int n){    // create a empty vector to store frequencies    // and initialize it by 0. Size of vector is    // maximum value (which is last value in sorted    // array) plus 1.    vector<int> freq(arr[n - 1] + 1, 0);     // Fill the vector with frequency    findFrequencyUtil(arr, 0, n - 1, freq);     // Print the frequencies    for (int i = 0; i <= arr[n - 1]; i++)        if (freq[i] != 0)            cout << "Element " << i << " occurs "                 << freq[i] << " times" << endl;} // Driver functionint main(){    int arr[] = { 1, 1, 1, 2, 3, 3, 5, 5,                  8, 8, 8, 9, 9, 10 };    int n = sizeof(arr) / sizeof(arr[0]);     findFrequency(arr, n);     return 0;}
// Java program to count number of occurrences of// each element in the array in less than O(n) timeimport java.util.*; class GFG {     // A recursive function to count number of occurrences    // for each element in the array without traversing    // the whole array    static void findFrequencyUtil(int arr[], int low,                                  int high, int[] freq)    {        // If element at index low is equal to element        // at index high in the array        if (arr[low] == arr[high]) {            // increment the frequency of the element            // by count of elements between high and low            freq[arr[low]] += high - low + 1;        }        else {            // Find mid and recurse for left and right            // subarray            int mid = (low + high) / 2;            findFrequencyUtil(arr, low, mid, freq);            findFrequencyUtil(arr, mid + 1, high, freq);        }    }     // A wrapper over recursive function    // findFrequencyUtil(). It print number of    // occurrences of each element in the array.    static void findFrequency(int arr[], int n)    {        // create a empty vector to store frequencies        // and initialize it by 0. Size of vector is        // maximum value (which is last value in sorted        // array) plus 1.        int[] freq = new int[arr[n - 1] + 1];         // Fill the vector with frequency        findFrequencyUtil(arr, 0, n - 1, freq);         // Print the frequencies        for (int i = 0; i <= arr[n - 1]; i++)            if (freq[i] != 0)                System.out.println("Element " + i + " occurs " + freq[i] + " times");    }     // Driver Code    public static void main(String[] args)    {        int arr[] = { 1, 1, 1, 2, 3, 3, 5,                      5, 8, 8, 8, 9, 9, 10 };        int n = arr.length;         findFrequency(arr, n);    }} // This code is contributed by 29AjayKumar
# Python 3 program to count number of occurrences of# each element in the array in less than O(n) time # A recursive function to count number of occurrences# for each element in the array without traversing# the whole arraydef findFrequencyUtil(arr, low, high, freq):         # If element at index low is equal to element    # at index high in the array    if (arr[low] == arr[high]):                 # increment the frequency of the element        # by count of elements between high and low        freq[arr[low]] += high - low + 1         else:                 # Find mid and recurse for left         # and right subarray        mid = int((low + high) / 2)        findFrequencyUtil(arr, low, mid, freq)        findFrequencyUtil(arr, mid + 1, high, freq)     # A wrapper over recursive function# findFrequencyUtil(). It print number of# occurrences of each element in the array.def findFrequency(arr, n):         # create a empty vector to store frequencies    # and initialize it by 0. Size of vector is    # maximum value (which is last value in sorted    # array) plus 1.    freq = [0 for i in range(n - 1 + 1)]         # Fill the vector with frequency    findFrequencyUtil(arr, 0, n - 1, freq)     # Print the frequencies    for i in range(0, arr[n - 1] + 1, 1):        if (freq[i] != 0):            print("Element", i, "occurs",                        freq[i], "times") # Driver Codeif __name__ == '__main__':    arr = [1, 1, 1, 2, 3, 3, 5,           5, 8, 8, 8, 9, 9, 10]    n = len(arr)    findFrequency(arr, n)     # This code is contributed by# Surendra_Gangwar
// C# program to count number of occurrences of// each element in the array in less than O(n) timeusing System; class GFG {     // A recursive function to count number of occurrences    // for each element in the array without traversing    // the whole array    static void findFrequencyUtil(int[] arr, int low,                                  int high, int[] freq)    {        // If element at index low is equal to element        // at index high in the array        if (arr[low] == arr[high]) {            // increment the frequency of the element            // by count of elements between high and low            freq[arr[low]] += high - low + 1;        }        else {            // Find mid and recurse for left and right            // subarray            int mid = (low + high) / 2;            findFrequencyUtil(arr, low, mid, freq);            findFrequencyUtil(arr, mid + 1, high, freq);        }    }     // A wrapper over recursive function    // findFrequencyUtil(). It print number of    // occurrences of each element in the array.    static void findFrequency(int[] arr, int n)    {        // create a empty vector to store frequencies        // and initialize it by 0. Size of vector is        // maximum value (which is last value in sorted        // array) plus 1.        int[] freq = new int[arr[n - 1] + 1];         // Fill the vector with frequency        findFrequencyUtil(arr, 0, n - 1, freq);         // Print the frequencies        for (int i = 0; i <= arr[n - 1]; i++)            if (freq[i] != 0)                Console.WriteLine("Element " + i + " occurs " + freq[i] + " times");    }     // Driver Code    public static void Main(String[] args)    {        int[] arr = { 1, 1, 1, 2, 3, 3, 5,                      5, 8, 8, 8, 9, 9, 10 };        int n = arr.Length;         findFrequency(arr, n);    }} // This code is contributed by Princi Singh
<script>// Javascript program to count number of occurrences of// each element in the array in less than O(n) time         // A recursive function to count number of occurrences    // for each element in the array without traversing    // the whole array    function findFrequencyUtil(arr, low, high, freq)    {             // If element at index low is equal to element        // at index high in the array        if (arr[low] == arr[high])        {                     // increment the frequency of the element            // by count of elements between high and low            freq[arr[low]] += high - low + 1;        }        else        {                     // Find mid and recurse for left and right            // subarray            let mid = Math.floor((low + high) / 2);            findFrequencyUtil(arr, low, mid, freq);            findFrequencyUtil(arr, mid + 1, high, freq);        }    }         // A wrapper over recursive function    // findFrequencyUtil(). It print number of    // occurrences of each element in the array.    function findFrequency(arr, n)    {             // create a empty vector to store frequencies        // and initialize it by 0. Size of vector is        // maximum value (which is last value in sorted        // array) plus 1.        let freq = new Array(arr[n - 1] + 1);                 for(let i = 0; i < arr[n - 1] + 1; i++)        {            freq[i] = 0;        }                 // Fill the vector with frequency        findFrequencyUtil(arr, 0, n - 1, freq);          // Print the frequencies        for (let i = 0; i <= arr[n - 1]; i++)            if (freq[i] != 0)                document.write("Element " + i + " occurs " + freq[i] + " times<br>");    }         // Driver Code    let arr = [1, 1, 1, 2, 3, 3, 5,                      5, 8, 8, 8, 9, 9, 10 ];    let n = arr.length;    findFrequency(arr, n);         // This code is contributed by rag2127.</script>
Element 1 occurs 3 times
Element 2 occurs 1 times
Element 3 occurs 2 times
Element 5 occurs 2 times
Element 8 occurs 3 times
Element 9 occurs 2 times
Element 10 occurs 1 times
Complexity Analysis: 
Time Complexity: O(m log n). Where m is the number of distinct elements in the array of size n. Since m <= M (a constant) (elements are in a limited range), the time complexity of this solution is O(log n).
Space Complexity:O(n). To store the elements in the HashMap O(n) extra space is needed.
Method 4 
In this method we use the same array as the hash map  by modifying it’s content.
Let’s dry run a example. 
arr = { 1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10 };
Step 1 :- Subtract 1 from each element of the array
arr  = {0 ,0 ,0 ,1 ,2 ,2 ,4 ,4 ,7 ,7 ,7 ,8 ,8 ,9 }
Step 2 :- Add n to the index at which the current array element points.
for example :- 
when i=0, arr[arr[0]%n] = 0 adding n to the arr[0], arr[0] =  14;
when i=1, arr[arr[1]%n] = 14 adding n to arr[0] ,arr[0] = 28;
Similarly finding the modified array in the same way we will get array as  
arr = {42 ,14 ,28 ,1 ,30, 2, 4, 46, 35, 21, 7, 8, 8, 9}
Step 3 :- Now in step 2 if you have noticed we added the n value to the index at which a particular element points to. So if we have more than one time have a element that point to the same index then in that case the division of the modified number with the n gives us the the frequency of the number. 
for example 
at i=0; arr[0] =42; arr[0] / n = 3 it means that 0  appeared three times in the modified array as you can see in the arr of step 1.
at i=1; arr[1] =14; arr[1]/14 = 1 it means that 1 appeared once in the modified array as you can see in the arr of step 1 .
and similarly for other values we can calculate.
C++
Java
C#
Javascript
// C++ program to count number of occurrences of// each element in the array #include <iostream>#include <bits/stdc++.h>using namespace std; // It prints number of occurrences of each element in the// array.void findFrequency(int input[], int n){    for (int i = 0; i < n; i++)        input[i]--;     for (int i = 0; i < n; i++)        input[input[i] % n] += n;     for (int i = 0; i < n; i++) {        if (input[i] / n)            cout << "Element " << (i + 1) << " occurs "                 << input[i] / n << " times" << endl;        // Change the element back to original value        input[i] = input[i] % n + 1;    }} // Driver functionint main(){    int arr[]        = { 1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10 };    int n = sizeof(arr) / sizeof(arr[0]);     findFrequency(arr, n);     return 0;} // This code is contributed by aditya kumar(adiyakumar129)
// Java program to count number of occurrences of each// element in the arrayimport java.io.*;import java.util.*;class GFG {     // It prints number of occurrences of each element in    // the array.    static void findFrequency(int[] input, int n)    {        for (int i = 0; i < n; i++)            input[i]--;         for (int i = 0; i < n; i++)            input[input[i] % n] += n;         for (int i = 0; i < n; i++) {            if ((input[i] / n) != 0)                System.out.println(                    "Element " + (i + 1) + " occurs "                    + input[i] / n + " times");            // Change the element back to original value            input[i] = input[i] % n + 1;        }    }     // Driver function    public static void main(String[] args)    {        int[] arr            = { 1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10 };        int n = arr.length;        findFrequency(arr, n);    }} // This code is contributed by aditya kumar(adiyakumar129)
// C# program to count number of occurrences of each element// in the arrayusing System; public class GFG {     // It prints number of occurrences of each element in    // the array.    static void findFrequency(int[] input, int n)    {        for (int i = 0; i < n; i++)            input[i]--;         for (int i = 0; i < n; i++)            input[input[i] % n] += n;         for (int i = 0; i < n; i++) {            if ((input[i] / n) != 0)                Console.WriteLine(                    "Element " + (i + 1) + " occurs "                    + input[i] / n + " times");            // Change the element back to original value            input[i] = input[i] % n + 1;        }    }     // Driver function    public static void Main(String[] args)    {        int[] arr            = { 1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10 };        int n = arr.Length;        findFrequency(arr, n);    }} // This code is contributed by shikhasingrajput
<script>// Javascript program to count number of occurrences of// each element in the array // It prints number of// occurrences of each element in the array.function findFrequency(input, n){    for (let i = 0; i < n; i++)        input[i]--;     for (let i = 0; i < n; i++)        input[input[i] % n] += n;     console.log(input)    for (let i = 0; i < n; i++) {        if (Math.floor(input[i] / n))            document.write("Element " + (i + 1) +            " occurs " + Math.floor(input[i] / n) + " times <br>");                     // Change the element back to original value        input[i] = input[i] % n + 1;    }} // Driver functionlet arr = [1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10];let n = arr.length; findFrequency(arr, n); // This code is contributed by Saurabh Jaiswal</script>
Element 1 occurs 3 times
Element 2 occurs 1 times
Element 3 occurs 2 times
Element 5 occurs 2 times
Element 8 occurs 3 times
Element 9 occurs 2 times
Element 10 occurs 1 times

https://youtu.be/B2hI-QPoisk This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
SURENDRA_GANGWAR
29AjayKumar
princi singh
andrew1234
SHUBHAMSINGH10
ukasp
avanitrachhadiya2155
rag2127
patel2127
satyaramanaidu
anushikasethh
shivanisinghss2110
adityakumar129
shikhasingrajput
_saurabh_jaiswal
Arrays
Divide and Conquer
Arrays
Divide and Conquer
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Merge Sort
QuickSort
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Program for Tower of Hanoi