Find even occurring elements in an array of limited range
Last Updated :
02 Nov, 2021
Given an array that contains odd number of occurrences for all numbers except for a few elements which are present even number of times. Find the elements which have even occurrences in the array in O(n) time complexity and O(1) extra space.
Assume array contain elements in the range 0 to 63.
Examples :
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
A simple method would be to traverse the array and store frequencies of its elements in a map. Later, print elements that have even count in the map. The solution takes O(n) time but requires extra space for storing frequencies. Below is an interesting method to solve this problem using bitwise operators.
This method assumes that long long integers are stored using 64 bits. The idea is to use XOR operator. We know that
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0
Consider below input -
1, 4, 7, 5, 9, 7, 3, 4, 6, 8, 3, 0, 3
If we left-shift 1 by value of each element of the array and take XOR of all the items, we will get below binary integer -
1101101011
Each 1 in the i'th index from the right represents odd occurrence of element i. And each 0 in the i'th index from the right represents even or non-occurrence of element i in the array.
0 is present in 2nd, 4th and 7th position in above binary number. But 2 is not present in our array. So our answer is 4 and 7.
Below is the implementation of above idea
C++
// C++ Program to find the even occurring elements
// in given array
#include <iostream>
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
<?php
// PHP Program to find the even
// occurring elements in given array
// Function to find the even
// occurring elements in given array
function printRepeatingEven($arr, $n)
{
$_xor = 0;
$pos;
// do for each element of array
for( $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 ($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
echo $arr[$i], " ";
// set bit as 1 to avoid
// printing duplicates
$_xor ^= $pos;
}
}
}
// Driver code
$arr = array(9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15 );
$n = sizeof($arr);
printRepeatingEven($arr, $n);
// This code is contributed by aj_36
?>
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
<script>
// Javascript Program to find the even occurring
// elements in given array
// Function to find the even occurring
// elements in given array
function printRepeatingEven(arr, n)
{
let _xor = 0;
let pos;
// do for each element of array
for (let 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 (let 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
document.write(arr[i] + " ");
// set bit as 1 to avoid
// printing duplicates
_xor ^= pos;
}
}
}
// Driver Code
let arr = [9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15];
let n = arr.length;
printRepeatingEven(arr, n);
</script>
Output :
12 23 15
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Missing Elements of a Range in an Array
Given an array of size N. Let min and max be the minimum and maximum in the array respectively. Task is to find how many number should be added to the given array such that all the element in the range [min, max] occur at-least once in the array.Examples: Input : arr[] = {4, 5, 3, 8, 6}Output : 1Onl
7 min read
Find the Kth occurrence of an element in a sorted Array
Given a sorted array arr[] of size N, an integer X, and a positive integer K, the task is to find the index of Kth occurrence of X in the given array. Examples: Input: N = 10, arr[] = [1, 2, 3, 3, 4, 5, 5, 5, 5, 5], X = 5, K = 2Output: Starting index of the array is '0' Second occurrence of 5 is at
15+ min read
Find missing elements of a range
Given an array, arr[0..n-1] of distinct elements and a range [low, high], find all numbers that are in a range, but not the array. The missing elements should be printed in sorted order.Examples: Input: arr[] = {10, 12, 11, 15}, low = 10, high = 15Output: 13, 14Input: arr[] = {1, 14, 11, 51, 15}, lo
15+ min read
Find frequency of each element in a limited range array in less than O(n) time
Given a sorted array arr[] of positive integers, the task is to find the frequency for each element in the array. Assume all elements in the array are less than some constant MNote: Do this without traversing the complete array. i.e. expected time complexity is less than O(n)Examples: Input: arr[] =
10 min read
Find the only non-repeating element in a given array
Given an array A[] consisting of N (1 ? N ? 105) positive integers, the task is to find the only array element with a single occurrence. Note: It is guaranteed that only one such element exists in the array. Examples: Input: A[] = {1, 1, 2, 3, 3}Output: 2Explanation: Distinct array elements are {1,
10 min read
Find elements larger than half of the elements in an array | Set 2
Given an array arr[] consisting of N positive integers, the task is to find the elements which are greater than at least half of the array elements. Examples: Input: arr[] = {1, 6, 3, 4}Output: 4 6Explanation:Size of the array is 4. The elements which are greater than atleast N/2(= 4/2 = 2) elements
7 min read
Maximize count of elements reaching the end of an Array
Given an array arr[] consisting of N integers, where each element denotes the maximum number of elements that can be placed on that index and an integer X, which denotes the maximum indices that can be jumped from an index, the task is to find the number of elements that can reach the end of the arr
15 min read
Find any one of the multiple repeating elements in read only array
Given a read-only array of size ( n+1 ), find one of the multiple repeating elements in the array where the array contains integers only between 1 and n. A read-only array means that the contents of the array canât be modified.Examples: Input : n = 5 arr[] = {1, 1, 2, 3, 5, 4} Output : One of the nu
15 min read
POTD Solutions | 1 Novâ 23 | Frequencies of Limited Range Array Elements
View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Frequency Counting algorithm but will also help you bui
7 min read
Find the missing number in a sorted array of limited range
Given a sorted array of size n, consisting of integers from 1 to n+1 with one missing. The task is to find the missing element.Examples: Input : arr[] = [1, 3, 4, 5, 6]Output : 2Input : arr[] = [1, 2, 3, 4, 5, 7, 8, 9, 10]Output : 6Input: arr[] = [1, 2, 3, 4]Output: 5Using Linear Search - O(n) time
11 min read