# Frequencies of Limited Range Array Elements

Tue Feb 08 2022 05:43:55 GMT+0000 (Coordinated Universal Time)

```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];
}
}
}```
content_copyCOPY

Frequencies of Limited Range Array Elements Given an array A[] of N positive integers which can contain integers from 1 to P where elements can be repeated or can be absent from the array. Your task is to count the frequency of all elements from 1 to N. Note: The elements greater than N in the array can be ignored for counting. Example 1: Input: N = 5 arr[] = {2, 3, 2, 3, 5} P = 5 Output: 0 2 2 0 1 Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 2 times. 3 occurring 2 times. 4 occurring 0 times. 5 occurring 1 time. Example 2: Input: N = 4 arr[] = {3,3,3,3} P = 3 Output: 0 0 4 0 Explanation: Counting frequencies of each array element We have: 1 occurring 0 times. 2 occurring 0 times. 3 occurring 4 times. 4 occurring 0 times. Your Task: Complete the function frequencycount() that takes the array and n as input parameters and modify the array in place to denote the frequency count of each element from 1 to N. i,e element at index 0 should represent the frequency of 1 and so on. Note: Can you solve this problem without using extra space (O(1) Space) ! Constraints: 1 ≤ N ≤ 10^5 1 ≤ P ≤ 4*10^4 1 <= A[i] <= P

https://practice.geeksforgeeks.org/problems/frequency-of-array-elements-1587115620/1/?track=DSASP-Arrays&batchId=190