📌  相关文章
📜  第一个元素在数组中出现偶数次

📅  最后修改于: 2021-05-31 22:21:37             🧑  作者: Mango

给定一个数组,找到在数组中出现偶数次的第一个元素。如果存在则返回元素,否则返回0。

例子:

一个简单的解决方案是逐个考虑每个元素。对于每个元素,开始计算频率,第一个元素的偶数等于结果。该解决方案的时间复杂度为O(n ^{2} )。

一个有效的解决方案可以使用具有O(n)时间和O(n)额外空间的哈希映射来解决此问题,如下所示:

    对于数组的每个元素a [i],请执行以下操作:
  • 如果哈希表中不存在a [i],则将其成对插入哈希表中(a [i],false)。
  • 如果哈希映射中存在a [i]且值为true,则将其切换为false。
  • 如果哈希图中存在a [i]且值为false,则将其切换为true。

    要找到第一个出现偶数次的元素,请再次遍历数组,找到值为true的a [i]并返回。

C++
// C++ code to find the first element
// that appears even number times
#include 
using namespace std;
int firstEven(int arr[], int n)
{
  
    unordered_map map1;
  
    for (int i = 0; i < n; i++)
    {
  
        // first time occurred
        if (map1.find(arr[i]) == map1.end())
            map1.insert(pair  (arr[i],false));
  
        // toggle for repeated occurrence
        else
        {
            bool val = map1.find(arr[i])->second;
            if (val == true)
                map1.find(arr[i])->second = false;
            else
                map1.find(arr[i])->second = true;
        }
    }
  
    int j = 0;
    for (j = 0; j < n; j++)
    {
  
        // first integer with true value
        if (map1.find(arr[j])->second == true)
            break;
    }
  
    return arr[j];
}
  
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 1, 6 };
    cout << firstEven(arr, 6);
    return 0;
}


Java
// JAVA code to find the first element
// that appears even number times
import java.util.*;
class GFG {
    public static int firstEven(int arr[], int n)
    {
  
        HashMap map = 
                 new HashMap();
  
        for (int i = 0; i < n; i++) {
  
            // first time occurred
            if (map.get(arr[i]) == null) 
                map.put(arr[i], false);
              
            // toggle for repeated occurrence
            else {
                boolean val = map.get(arr[i]);
                if (val == true)
                    map.put(arr[i], false);
                else
                    map.put(arr[i], true);
            }
        }
  
        int j = 0;
        for (j = 0; j < n; j++) {
  
            // first integer with true value
            if (map.get(arr[j]) == true)
                break;
        }
  
        return arr[j];
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 4, 6, 8, 1, 6 };
        int n = arr.length;
        System.out.println(firstEven(arr, n));
    }
}


Python3
# Python3 code to find the first element 
# that appears even number times 
def firstEven(arr, n): 
  
    map1 = {} 
    for i in range(0, n): 
      
        # first time occurred 
        if arr[i] not in map1: 
            map1[arr[i]] = False
  
        # toggle for repeated occurrence 
        else:
            map1[arr[i]] = not map1[arr[i]]
  
    for j in range(0, n): 
      
        # first integer with true value 
        if map1[arr[j]] == True: 
            break
      
    return arr[j] 
  
# Driver code 
if __name__ == "__main__":
  
    arr = [2, 4, 6, 8, 1, 6] 
    print(firstEven(arr, 6))
  
# This code is contributed 
# by Rituraj Jain


C#
// C# code to find the first element
// that appears even number times
using System;
using System.Collections.Generic;
  
class GFG 
{
    static int firstEven(int []arr, 
                         int n)
    {
        var map = new Dictionary();
          
        var hash = new HashSet(arr);
          
        foreach (int a in hash)
            map.Add(a, "null");
              
        for (int i = 0; i < n; i++) 
        {
  
            // first time occurred
            if (map[arr[i]].Equals("null"))
                map[arr[i]] = "false";
              
            // toggle for repeated
            // occurrence
            else 
            {
                string val = map[arr[i]];
                if (val.Equals("true"))
                    map[arr[i]] = "false";
                else
                    map[arr[i]] = "true";
            }
        }
  
        int j = 0;
        for (j = 0; j < n; j++)
        {
  
            // first integer with
            // true value
            if (map[arr[j]].Equals("true"))
                break;
        }
        return arr[j];
    }
  
    // Driver code
    static void Main()
    {
        int []arr = new int[]{ 2, 4, 6,
                               8, 1, 6 };
        int n = arr.Length;
        Console.Write(firstEven(arr, n));
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


输出:
6

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

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