给定一个数组,其中包含所有数字的奇数个出现次数,除了少数元素出现偶数次之外。在O(n)时间复杂度和O(1)额外空间中找到在数组中偶数出现的元素。
假设数组包含0到63范围内的元素。
例子 :
Input: [9, 12, 23, 10, 12, 12, 15, 23, 14, 12, 15]
Output: 12, 23 and 15
Input: [1, 4, 7, 5, 9, 7, 3, 4, 6, 8, 3, 0, 3]
Output: 4 and 7
Input: [4, 4, 10, 10, 4, 4, 4, 4, 10, 10]
Output: 4 and 10
一种简单的方法是遍历数组并将其元素的频率存储在地图中。以后,打印甚至在地图中计数的元素。该解决方案花费O(n)时间,但需要额外的空间来存储频率。下面是一种使用按位运算运算符解决此问题的有趣方法。
此方法假定使用64位存储long long整数。这个想法是使用XOR运算符。我们知道
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0
考虑以下输入–
1, 4, 7, 5, 9, 7, 3, 4, 6, 8, 3, 0, 3
如果我们将数组的每个元素的值左移1,并对所有项进行XOR,我们将得到二进制整数以下的值–
1101101011
从右起第i个索引中的每个1表示元素i的奇数出现。从右起第i个索引中的每个0表示数组中元素i的偶数或不出现。
在上述二进制数的第二,第四和第七位置中存在0。但是2在我们的数组中不存在。因此,我们的答案是4和7。
以下是上述想法的实现
C++
// C++ Program to find the even occurring elements
// in given array
#include
using namespace std;
// Function to find the even occurring elements
// in given array
void printRepeatingEven(int arr[], int n)
{
long long _xor = 0L;
long long pos;
// do for each element of array
for( int i = 0; i < n; ++i)
{
// left-shift 1 by value of current element
pos = 1 << arr[i];
// Toggle the bit everytime element gets repeated
_xor ^= pos;
}
// Traverse array again and use _xor to find even
// occurring elements
for (int i = 0; i < n; ++i)
{
// left-shift 1 by value of current element
pos = 1 << arr[i];
// Each 0 in _xor represents an even occurrence
if (!(pos & _xor))
{
// print the even occurring numbers
cout << arr[i] << " ";
// set bit as 1 to avoid printing duplicates
_xor ^= pos;
}
}
}
// Driver code
int main()
{
int arr[] = { 9, 12, 23, 10, 12, 12, 15, 23,
14, 12, 15 };
int n = sizeof(arr) / sizeof(arr[0]);
printRepeatingEven(arr, n);
return 0;
}
Java
// Java Program to find the even occurring
// elements in given array
class GFG
{
// Function to find the even occurring
// elements in given array
static void printRepeatingEven(int arr[],
int n)
{
long _xor = 0L;
long pos;
// do for each element of array
for (int i = 0; i < n; ++i)
{
// left-shift 1 by value of
// current element
pos = 1 << arr[i];
// Toggle the bit everytime
// element gets repeated
_xor ^= pos;
}
// Traverse array again and use _xor
// to find even occurring elements
for (int i = 0; i < n; ++i)
{
// left-shift 1 by value of
// current element
pos = 1 << arr[i];
// Each 0 in _xor represents
// an even occurrence
if (!((pos & _xor)!=0))
{
// print the even occurring numbers
System.out.print(arr[i] + " ");
// set bit as 1 to avoid
// printing duplicates
_xor ^= pos;
}
}
}
// Driver code
public static void main(String args[])
{
int arr[] = {9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15};
int n = arr.length;
printRepeatingEven(arr, n);
}
}
// This code is contributed
// by 29AjayKumar
C#
// C# Program to find the even occurring
// elements in given array
using System;
class GFG
{
// Function to find the even occurring
// elements in given array
static void printRepeatingEven(int[] arr,
int n)
{
long _xor = 0L;
long pos;
// do for each element of array
for (int i = 0; i < n; ++i)
{
// left-shift 1 by value of
// current element
pos = 1 << arr[i];
// Toggle the bit everytime
// element gets repeated
_xor ^= pos;
}
// Traverse array again and use _xor
// to find even occurring elements
for (int i = 0; i < n; ++i)
{
// left-shift 1 by value of
// current element
pos = 1 << arr[i];
// Each 0 in _xor represents
// an even occurrence
if (!((pos & _xor) != 0))
{
// print the even occurring numbers
Console.Write(arr[i] + " ");
// set bit as 1 to avoid
// printing duplicates
_xor ^= pos;
}
}
}
// Driver code
public static void Main()
{
int[] arr = {9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15};
int n = arr.Length;
printRepeatingEven(arr, n);
}
}
// This code is contributed
// by Mukul singh
PHP
Python 3
# Python 3 program to find the even
# occurring elements in given array
# Function to find the even occurring
# elements in given array
def printRepeatingEven(arr, n):
axor = 0;
# do for each element of array
for i in range(0, n):
# left-shift 1 by value of
# current element
pos = 1 << arr[i];
# Toggle the bit every time
# element gets repeated
axor ^= pos;
# Traverse array again and use _xor
# to find even occurring elements
for i in range(0, n - 1):
# left-shift 1 by value of
# current element
pos = 1 << arr[i];
# Each 0 in _xor represents an
# even occurrence
if (not(pos & axor)):
# print the even occurring numbers
print(arr[i], end = " ");
# set bit as 1 to avoid printing
# duplicates
axor ^= pos;
# Driver code
arr = [9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15 ];
n = len(arr) ;
printRepeatingEven(arr, n);
# This code is contributed
# by Shivi_Aggarwal
Javascript
输出 :
12 23 15