DSA Day 31/100

DSA Day 31/100

Topic: Arrays

Questions Successfully Completed: 1

1) Frequencies of Limited Range Array Elements

Easy

Frequencies of Limited Range Array Elements

Time Complexity : O(N)

Space Complexity: O(N)

Question
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.
 public static void frequencyCount(int arr[], int N, int P)
    {
      int[] freq_hash = new int[N+1];

      for(int i=0;i<N;i++){
          if(arr[i]>N){continue;}
          else{
          freq_hash[arr[i]]++;}
      }
      for(int i=0;i<N;i++)
      {
          arr[i]=freq_hash[i+1];
      }
    }

// OUTPUT 
// 1 3 1 0 0

Thank you for reading :)