Snippets Collections
class Solution{
    //Function to count the frequency of all elements from 1 to N in the array.
    public static void frequencyCount(int arr[], int N, int P)
    {
        //Decreasing all elements by 1 so that the elements
        //become in range from 0 to n-1.
        int maxi = Math.max(P,N);
        int count[] = new int[maxi+1];
        Arrays.fill(count, 0);
        for(int i=0;i<N;i++){
            count[arr[i]]++; 
        }
        
        for(int i=0;i<N;i++){
            arr[i] = count[i+1];
        }
    }
}
star

Tue Feb 08 2022 05:43:55 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/frequency-of-array-elements-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #frequencycount #frequencies #limitedrange

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension