Check whether the frequency of the elements in the Array is unique or not
Last Updated :
17 Oct, 2023
Given an array arr[] of N integers, the task is to check whether the frequency of the elements in the array is unique or not, or in other words, there are no two distinct numbers in an array with equal frequency. If all the frequency is unique then Print “YES”, else Print “NO”.
Examples:
Input: N = 5, arr = [1, 1, 2, 5, 5]
Output: false
Explanation: The array contains 2 (1’s), 1 (2’s), and 2 (5’s) since the 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: true
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 satisfies the condition.
Approach: To solve the problem follow the below idea:
Calculate the frequency of each element of the array and check whether all the frequencies are unique or not.
Follow the steps to solve the problem:
- Calculate the frequency of each element and store it in the map.
- Now we have to check the frequencies present in the map are unique or not.
- To do so we can insert all the frequencies in the set and compare the size of the set and map.
- As the set store only unique elements so if the size of the set and the size of the map is equal then our answer is yes else our answer is no because there is at least one frequency that is repeating.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isFrequencyUnique( int n, int arr[])
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp[arr[i]]++;
}
unordered_set< int > st;
for ( auto x : mp) {
st.insert(x.second);
}
return st.size() == mp.size();
}
int main()
{
int arr[] = { 2, 2, 5, 10, 1, 2, 10, 5, 10, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
if (isFrequencyUnique(n, arr)) {
cout << "YES\n" ;
}
else {
cout << "NO\n" ;
}
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean isFrequencyUnique( int n, int [] arr)
{
Map<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
mp.put(arr[i], mp.getOrDefault(arr[i], 0 ) + 1 );
}
Set<Integer> st = new HashSet<>();
for (Map.Entry<Integer, Integer> entry :
mp.entrySet()) {
st.add(entry.getValue());
}
return st.size() == mp.size();
}
public static void main(String[] args)
{
int [] arr = { 2 , 2 , 5 , 10 , 1 , 2 , 10 , 5 , 10 , 2 };
int n = arr.length;
if (isFrequencyUnique(n, arr)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python
def isFrequencyUnique(arr):
freq_dict = {}
for num in arr:
if num in freq_dict:
freq_dict[num] + = 1
else :
freq_dict[num] = 1
unique_freqs = set (freq_dict.values())
return len (unique_freqs) = = len (freq_dict)
arr = [ 2 , 2 , 5 , 10 , 1 , 2 , 10 , 5 , 10 , 2 ]
if isFrequencyUnique(arr):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class Program
{
static bool IsFrequencyUnique( int n, int [] arr)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else
{
mp[arr[i]] = 1;
}
}
HashSet< int > st = new HashSet< int >();
foreach ( var x in mp)
{
st.Add(x.Value);
}
return st.Count == mp.Count;
}
static void Main( string [] args)
{
int [] arr = { 2, 2, 5, 10, 1, 2, 10, 5, 10, 2 };
int n = arr.Length;
if (IsFrequencyUnique(n, arr))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function isFrequencyUnique(arr) {
const frequencyMap = new Map();
for (const num of arr) {
if (!frequencyMap.has(num)) {
frequencyMap.set(num, 0);
}
frequencyMap.set(num, frequencyMap.get(num) + 1);
}
const uniqueFrequencies = new Set();
for (const freq of frequencyMap.values()) {
uniqueFrequencies.add(freq);
}
return uniqueFrequencies.size === frequencyMap.size;
}
const arr = [2, 2, 5, 10, 1, 2, 10, 5, 10, 2];
if (isFrequencyUnique(arr)) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time Complexity: O(N), we just need to traverse the array and map for once only.
Auxillary space: O(N), we need a frequency map and set to check the frequency of elements is unique or not.
Share your thoughts in the comments
Please Login to comment...