Find element in array with frequency equal to sum of frequencies of other elements

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, Frequency of the element 3 is equal to 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 case of even length array calculate the frequency of each element of the array and then finally, check for any element of the array have a frequency equal to the half-length of the array.



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the
// element whose frequency is equal 
// to the sum of frequencies of 
// other elements of the array
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check that any element 
// have frequency equal to the sum of 
// frequency of other elements of the array
bool isFrequencyEqual(int arr[], int len)
{
    // Check that if the array length
    // is odd, Then no solution possible
    if (len % 2 == 1){
        cout << "No Such Element";
        return false;
    }
  
    // Hash-map to store the frequency
    // of elements of array
    map<int, int> freq;
      
    // Loop to find the frequency 
    // of elements of array
    for (int i = 0; i < len; i++)
        freq[arr[i]]++;
          
    // Loop to check if any element 
    // of the array have frequency 
    // equal to the half length
    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;
}
  
// Driver Code
int main()
{
    int arr[6] = { 1, 2, 2, 3, 3, 3 };
    int n = 6;
      
    // Function Call
    isFrequencyEqual(arr, n);
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the
// element whose frequency is equal 
// to the sum of frequencies of 
// other elements of the array
import java.util.*;
  
class GFG{
   
// Function to check that any element 
// have frequency equal to the sum of 
// frequency of other elements of the array
static boolean isFrequencyEqual(int arr[], int len)
{
    // Check that if the array length
    // is odd, Then no solution possible
    if (len % 2 == 1){
        System.out.print("No Such Element");
        return false;
    }
   
    // Hash-map to store the frequency
    // of elements of array
    HashMap<Integer,Integer> freq = new HashMap<Integer,Integer>();
       
    // Loop to find the frequency 
    // of elements of array
    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);
        }
           
    // Loop to check if any element 
    // of the array have frequency 
    // equal to the half length
    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;
}
   
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 3, 3, 3 };
    int n = 6;
       
    // Function Call
    isFrequencyEqual(arr, n);
}
}
  
// This code is contributed by Rajput-Ji
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find the
// element whose frequency is equal 
// to the sum of frequencies of 
// other elements of the array
using System;
using System.Collections.Generic;
  
class GFG{
    
// Function to check that any element 
// have frequency equal to the sum of 
// frequency of other elements of the array
static bool isFrequencyEqual(int []arr, int len)
{
    // Check that if the array length
    // is odd, Then no solution possible
    if (len % 2 == 1){
        Console.Write("No Such Element");
        return false;
    }
    
    // Hash-map to store the frequency
    // of elements of array
    Dictionary<int,int> freq = new Dictionary<int,int>();
        
    // Loop to find the frequency 
    // of elements of array
    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);
        }
            
    // Loop to check if any element 
    // of the array have frequency 
    // equal to the half length
    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;
}
    
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 2, 3, 3, 3 };
    int n = 6;
        
    // Function Call
    isFrequencyEqual(arr, n);
}
}
  
// This code is contributed by Rajput-Ji
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find the
# element whose frequency is equal 
# to the sum of frequencies of 
# other elements of the array
  
# Function to check that any element 
# have frequency equal to the sum of 
# frequency of other elements of the array
def isFrequencyEqual(arr, length) :
  
    # Check that if the array length
    # is odd, Then no solution possible
    if (length % 2 == 1) :
        print("No Such Element");
        return False;
  
    # Hash-map to store the frequency
    # of elements of array
    freq = dict.fromkeys(arr, 0);
      
    # Loop to find the frequency 
    # of elements of array
    for i in range(length) :
        freq[arr[i]] += 1;
          
    # Loop to check if any element 
    # of the array have frequency 
    # equal to the half length
    for i in range(length) :
        if (freq[arr[i]] == length / 2) :
            print(arr[i]);
            return True;
          
    print("No such element",end="");
    return False;
  
# Driver Code
if __name__ == "__main__" :
  
    arr = [ 1, 2, 2, 3, 3, 3 ];
    n = 6;
      
    # Function Call
    isFrequencyEqual(arr, n);
      
# This code is contributed by Yash_R
chevron_right

Output:
3

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, Yash_R

Article Tags :
Practice Tags :