Find element in array with frequency equal to sum of frequencies of other elements
Last Updated :
03 May, 2023
Given an integer array arr[], the task is to find an element in the array whose frequency is equal to the sum of frequencies of other elements of the array.
Examples:
Input: arr[] = {1, 2, 2, 3, 3, 3}
Output: 3
Explanation:
Frequencies of elements of the array –
Frequency(3) = 3
Frequency(2) = 2
Frequency(1) = 1
Here, the frequency of element 3 is equal to the
the sum of frequencies of other elements of the array.
Input: arr[] = {1, 2, 3}
Output: -1
Explanation:
In the above-given array, there is no such
element whose frequency is equal to the sum of
frequencies of other elements of the array.
Approach: The key observation in the problem is if the length of the array is odd, then there will be no such element, whereas, in the case of an even length array, calculate the frequency of each element of the array and then finally, check for any element of the array that has a frequency equal to the half-length of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isFrequencyEqual( int arr[], int len)
{
if (len % 2 == 1){
cout << "No Such Element" ;
return false ;
}
map< int , int > freq;
for ( int i = 0; i < len; i++)
freq[arr[i]]++;
for ( int i = 0; i < len; i++){
if (freq[arr[i]] == len / 2){
cout << arr[i] << endl;
return true ;
}
}
cout << "No such element" ;
return false ;
}
int main()
{
int arr[6] = { 1, 2, 2, 3, 3, 3 };
int n = 6;
isFrequencyEqual(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isFrequencyEqual( int arr[], int len)
{
if (len % 2 == 1 ){
System.out.print( "No Such Element" );
return false ;
}
HashMap<Integer,Integer> freq = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < len; i++)
if (freq.containsKey(arr[i])){
freq.put(arr[i], freq.get(arr[i])+ 1 );
}
else {
freq.put(arr[i], 1 );
}
for ( int i = 0 ; i < len; i++){
if (freq.containsKey(arr[i]) && freq.get(arr[i]) == len / 2 ){
System.out.print(arr[i] + "\n" );
return true ;
}
}
System.out.print( "No such element" );
return false ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 3 , 3 , 3 };
int n = 6 ;
isFrequencyEqual(arr, n);
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool isFrequencyEqual( int []arr, int len)
{
if (len % 2 == 1){
Console.Write( "No Such Element" );
return false ;
}
Dictionary< int , int > freq = new Dictionary< int , int >();
for ( int i = 0; i < len; i++)
if (freq.ContainsKey(arr[i])){
freq[arr[i]] = freq[arr[i]]+1;
}
else {
freq.Add(arr[i], 1);
}
for ( int i = 0; i < len; i++){
if (freq.ContainsKey(arr[i]) && freq[arr[i]] == len / 2){
Console.Write(arr[i] + "\n" );
return true ;
}
}
Console.Write( "No such element" );
return false ;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 2, 3, 3, 3 };
int n = 6;
isFrequencyEqual(arr, n);
}
}
|
Python3
def isFrequencyEqual(arr, length) :
if (length % 2 = = 1 ) :
print ( "No Such Element" );
return False ;
freq = dict .fromkeys(arr, 0 );
for i in range (length) :
freq[arr[i]] + = 1 ;
for i in range (length) :
if (freq[arr[i]] = = length / 2 ) :
print (arr[i]);
return True ;
print ( "No such element" ,end = "");
return False ;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 3 , 3 , 3 ];
n = 6 ;
isFrequencyEqual(arr, n);
|
Javascript
<script>
function isFrequencyEqual(arr, len)
{
if (len % 2 == 1){
document.write( "No Such Element" );
return false ;
}
var freq = {};
for ( var i = 0; i < len; i++)
freq[arr[i]] = 0;
for ( var i = 0; i < len; i++)
freq[arr[i]]++;
for ( var i = 0; i < len; i++){
if (freq[arr[i]] == len / 2){
document.write(arr[i]);
return true ;
}
}
document.write( "No such element" );
return false ;
}
var arr = [ 1, 2, 2, 3, 3, 3];
var n = 6;
isFrequencyEqual(arr, n);
</script>
|
Time Complexity: O(NLogN)
Space Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...