📌  相关文章
📜  查找与其他数组元素具有不同频率的元素

📅  最后修改于: 2021-06-01 01:42:21             🧑  作者: Mango

给定一个由N个整数组成的数组。除一个元素外,数组中的每个元素发生相同的次数。任务是找到此元素。

例子:

Input : arr[] = {1, 1, 2, 2, 3}
Output : 3

Input : arr[] = {0, 1, 2, 4, 4}
Output : 4

这个想法是使用哈希表频率来存储给定元素的频率。一旦我们在哈希表中有了频率,就可以遍历该表以找到唯一与其他频率不同的值。

下面是上述想法的实现:

C++
// C++ program to find the element having
// different frequency than other array
// elements having same frequency
#include 
using namespace std;
  
// Function to find the element having
// different frequency from other array
// elements with same frequency
int findElement(int arr[], int n)
{
    // Store frequencies of elements
    unordered_map freq;
    for (int i = 0; i < n; i++) {
  
        // increase the value by 1 for every
        // time the element occurs in an array
        freq[arr[i]]++;
    }
  
    // Below code is used find the only different
    // value in freq. 
    auto it = freq.begin();
    int fst_fre = it->second, fst_ele = it->first;
    if (freq.size() <= 2)
        return fst_fre;
    it++;
    int sec_fre = it->second, sec_ele = it->first;
    it++;
    int trd_fre = it->second, trd_ele = it->first;
    if (sec_fre == fst_fre && sec_fre != trd_fre)
        return trd_ele;
    if (sec_fre == trd_fre && sec_fre != fst_fre)
        return fst_ele;
    if (fst_fre == trd_fre && sec_fre != fst_fre)
        return sec_ele;
  
    // We reach here when first three frequencies are same
    it++;
    for (; it != freq.end(); it++) {
        if (it->second != fst_fre)
            return it->first;
    }
  
    return -1;
}
  
// Driver code
int main()
{
    int arr[] = { 0, 1, 2, 4, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findElement(arr, n) << endl;
    return 0;
}


Python3
# Python program to find the element having 
# different frequency than other array 
# elements having same frequency 
  
# Function for above implementation
def findElement(arr, n) :
      
    # Empty dictionary to hold the values
    freq = {}
      
    # Initialization of frequencies of each 
    # element to 0
    for i in range(0, n) :
          
        freq[arr[i]] = 0
          
    # Count of frequencies of elements
    for i in range(0, n) :
          
        freq[arr[i]] = freq[arr[i]] + 1
       
    # Storing the first value of dictionary
    trd_ele = freq[0]
      
    # Variable to hold the final result
    position = -1
      
    # Following loop iterates through the dictionary
    # and checks if frequencies are different 
    # from the frequency of the first element
    for i in freq :
          
        flag = freq[i]
          
        if trd_ele != flag :
              
            # Difference has been detected
            position = i
            break
              
    # Following lines of code checks if the first
    # element is itself the required anomaly by 
    # comparing the frequencies of first 3 elements
    fst_ele = freq[1]
    sec_ele = freq[2]
      
    if trd_ele != fst_ele :
          
        if trd_ele != sec_ele :
              
            for i in freq :
                  
                # First element is the desired result
                position = i
                break
      
    # Final result is returned
    return position
      
  
# Driver code
arr = [ 0, 1, 2, 4, 4 ]
# Variable to store length of array
n = len(arr)
print (findElement(arr, n))
  
# This code is contributed by Pratik Basu


输出:
4

时间复杂度:O(n)
辅助空间:O(n)

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”