# Check if frequency of each element in given array is unique or not

• Last Updated : 10 Jun, 2021

Given an array arr[] of N positive integers where the integers are in the range from 1 to N, the task is to check whether the frequency of the elements in the array is unique or not. If all the frequency is unique then print “Yes”, else print “No”.

Examples:

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII

Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!

Input: N = 5, arr[] = {1, 1, 2, 5, 5}
Output: No
Explanation:
The array contains 2 (1’s), 1 (2’s) and 2 (5’s), since the number of frequency of 1 and 5 are the same i.e. 2 times. Therefore, this array does not satisfy the condition.

Input: N = 10, arr[] = {2, 2, 5, 10, 1, 2, 10, 5, 10, 2}
Output: Yes
Explanation:
Number of 1’s -> 1
Number of 2’s -> 4
Number of 5’s -> 2
Number of 10’s -> 3.
Since, the number of occurrences of elements present in the array is unique. Therefore, this array does not satisfy the condition.

Naive Approach: The idea is to check for every number from 1 to N whether it is present in the array or not. If yes, then count the frequency of that element in the array, and store the frequency in an array. At last, just check for any duplicate element in the array and print the output accordingly.

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The idea is to use Hashing. Below are the steps:

1. Traverse the given array arr[] and store the frequency of each element in a Map.
2. Now traverse the map and check if the count of any element occurred more than once.
3. If the count of any element in the above steps is more than one then print “No”, else print “Yes”.

Below is the implementation of the above approach:

## C++

 `// C++ code for the above approach``#include ``using` `namespace` `std;` `// Function to check whether the``// frequency of elements in array``// is unique or not.``bool` `checkUniqueFrequency(``int` `arr[],``                          ``int` `n)``{` `    ``// Freq map will store the frequency``    ``// of each element of the array``    ``unordered_map<``int``, ``int``> freq;` `    ``// Store the frequency of each``    ``// element from the array``    ``for` `(``int` `i = 0; i < n; i++) {``        ``freq[arr[i]]++;``    ``}` `    ``unordered_set<``int``> uniqueFreq;` `    ``// Check whether frequency of any``    ``// two or more elements are same``    ``// or not. If yes, return false``    ``for` `(``auto``& i : freq) {``        ``if` `(uniqueFreq.count(i.second))``            ``return` `false``;``        ``else``            ``uniqueFreq.insert(i.second);``    ``}` `    ``// Return true if each``    ``// frequency is unique``    ``return` `true``;``}` `// Driver Code``int` `main()``{``    ``// Given array arr[]``    ``int` `arr[] = { 1, 1, 2, 5, 5 };``    ``int` `n = ``sizeof` `arr / ``sizeof` `arr[0];` `    ``// Function Call``    ``bool` `res = checkUniqueFrequency(arr, n);` `    ``// Print the result``    ``if` `(res)``        ``cout << ``"Yes"` `<< endl;``    ``else``        ``cout << ``"No"` `<< endl;``    ``return` `0;``}`

## Java

 `// Java code for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to check whether the``// frequency of elements in array``// is unique or not.``static` `boolean` `checkUniqueFrequency(``int` `arr[],``                                    ``int` `n)``{``    ` `    ``// Freq map will store the frequency``    ``// of each element of the array``    ``HashMap freq = ``new` `HashMap();` `    ``// Store the frequency of each``    ``// element from the array``    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``        ``if``(freq.containsKey(arr[i]))``        ``{``            ``freq.put(arr[i], freq.get(arr[i]) + ``1``);``        ``}``else``        ``{``            ``freq.put(arr[i], ``1``);``        ``}``    ``}` `    ``HashSet uniqueFreq = ``new` `HashSet();` `    ``// Check whether frequency of any``    ``// two or more elements are same``    ``// or not. If yes, return false``    ``for``(Map.Entry i : freq.entrySet())``    ``{``        ``if` `(uniqueFreq.contains(i.getValue()))``            ``return` `false``;``        ``else``            ``uniqueFreq.add(i.getValue());``    ``}` `    ``// Return true if each``    ``// frequency is unique``    ``return` `true``;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given array arr[]``    ``int` `arr[] = { ``1``, ``1``, ``2``, ``5``, ``5` `};``    ``int` `n = arr.length;` `    ``// Function call``    ``boolean` `res = checkUniqueFrequency(arr, n);` `    ``// Print the result``    ``if` `(res)``        ``System.out.print(``"Yes"` `+ ``"\n"``);``    ``else``        ``System.out.print(``"No"` `+ ``"\n"``);``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 code for``# the above approach``from` `collections ``import` `defaultdict` `# Function to check whether the``# frequency of elements in array``# is unique or not.``def` `checkUniqueFrequency(arr, n):`` ` `    ``# Freq map will store the frequency``    ``# of each element of the array``    ``freq ``=` `defaultdict (``int``)`` ` `    ``# Store the frequency of each``    ``# element from the array``    ``for` `i ``in` `range` `(n):``        ``freq[arr[i]] ``+``=` `1`` ` `    ``uniqueFreq ``=` `set``([])`` ` `    ``# Check whether frequency of any``    ``# two or more elements are same``    ``# or not. If yes, return false``    ``for` `i ``in` `freq:``        ``if` `(freq[i] ``in` `uniqueFreq):``            ``return` `False``        ``else``:``            ``uniqueFreq.add(freq[i])`` ` `    ``# Return true if each``    ``# frequency is unique``    ``return` `True`` ` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Given array arr[]``    ``arr ``=` `[``1``, ``1``, ``2``, ``5``, ``5``]``    ``n ``=` `len``(arr)`` ` `    ``# Function Call``    ``res ``=` `checkUniqueFrequency(arr, n)`` ` `    ``# Print the result``    ``if` `(res):``        ``print` `(``"Yes"``)``    ``else``:``        ``print` `(``"No"``)` `# This code is contributed by Chitranayal`

## C#

 `// C# code for the above approach``using` `System;``using` `System.Collections.Generic;``class` `GFG{` `// Function to check whether the``// frequency of elements in array``// is unique or not.``static` `bool` `checkUniqueFrequency(``int` `[]arr,``                                 ``int` `n)``{``    ` `    ``// Freq map will store the frequency``    ``// of each element of the array``    ``Dictionary<``int``,``               ``int``> freq = ``new` `Dictionary<``int``,``                                          ``int``>();` `    ``// Store the frequency of each``    ``// element from the array``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``if``(freq.ContainsKey(arr[i]))``        ``{``            ``freq[arr[i]] = freq[arr[i]] + 1;``        ``}``else``        ``{``            ``freq.Add(arr[i], 1);``        ``}``    ``}` `    ``HashSet<``int``> uniqueFreq = ``new` `HashSet<``int``>();` `    ``// Check whether frequency of any``    ``// two or more elements are same``    ``// or not. If yes, return false``    ``foreach``(KeyValuePair<``int``,``                         ``int``> i ``in` `freq)``    ``{``        ``if` `(uniqueFreq.Contains(i.Value))``            ``return` `false``;``        ``else``            ``uniqueFreq.Add(i.Value);``    ``}` `    ``// Return true if each``    ``// frequency is unique``    ``return` `true``;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given array []arr``    ``int` `[]arr = { 1, 1, 2, 5, 5 };``    ``int` `n = arr.Length;` `    ``// Function call``    ``bool` `res = checkUniqueFrequency(arr, n);` `    ``// Print the result``    ``if` `(res)``        ``Console.Write(``"Yes"` `+ ``"\n"``);``    ``else``        ``Console.Write(``"No"` `+ ``"\n"``);``}``}` `// This code is contributed by sapnasingh4991`

## Javascript

 ``
Output:
`No`

Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up