给定一个整数数组arr[] ,任务是在数组中找到一个元素,该元素的频率等于该数组其他元素的频率之和。
例子:
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.
方法:问题中的关键观察是如果数组的长度是奇数,那么就不会有这样的元素,而在偶数长度数组的情况下,计算数组中每个元素的频率,然后最后,检查数组中频率等于数组半长的任何元素。
下面是上述方法的实现:
C++
// C++ implementation to find the
// element whose frequency is equal
// to the sum of frequencies of
// other elements of the array
#include
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 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;
}
Java
// 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 freq = new HashMap();
// 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
C#
// 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 freq = new Dictionary();
// 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
Python3
# 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
Javascript
输出:
3
时间复杂度: O(N)
空间复杂度: O(N)