Find the elements appearing even number of times in an Array
Given an array arr[] consisting of N positive numbers in the range [1, N], the task is to print the array elements which appear even number of times in the given array.
Example :
Input: N = 8, arr[] = {4, 4, 2, 4, 8, 2, 3, 4}
Output: [2, 4]
Explanation: The numbers 2 and 4 appear even number of times in the array.
Input: N=4, arr[] = {3, 1, 4, 4}
Output: [4]
Naive Approach: The simplest approach to solve the problem is to store the frequency of each array element in a Hashmap and print the elements having even frequency.
C++
#include <bits/stdc++.h>
using namespace std;
void findDuplicates( int a[], int n)
{
unordered_map< int , int > hashMap;
for ( int i = 0; i < n; i++)
hashMap[a[i]]++;
vector< int > reqdAns;
for ( auto & it : hashMap) {
if (it.second % 2 == 0)
reqdAns.push_back(it.first);
}
for ( int i = 0; i < reqdAns.size(); i++)
cout << reqdAns[i] << " " ;
}
int main()
{
int a[] = { 1, 1, 1, 3, 1, 2, 8, 2 };
findDuplicates(a, 8);
}
|
Java
import java.util.*;
public class Main {
static void findDuplicates( int [] a, int n)
{
Map<Integer, Integer> hashMap = new HashMap<>();
for ( int i = 0 ; i < n; i++)
hashMap.put(a[i], hashMap.getOrDefault(a[i], 0 ) + 1 );
List<Integer> reqdAns = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : hashMap.entrySet()) {
if (entry.getValue() % 2 == 0 )
reqdAns.add(entry.getKey());
}
for ( int i = 0 ; i < reqdAns.size(); i++)
System.out.print(reqdAns.get(i) + " " );
}
public static void main(String[] args) {
int [] a = { 1 , 1 , 1 , 3 , 1 , 2 , 8 , 2 };
findDuplicates(a, 8 );
}
}
|
Python3
def findElements(a):
hashMap = {}
for i in range ( len (a)):
if a[i] in hashMap:
hashMap[a[i]] + = 1
else :
hashMap[a[i]] = 1
ans = []
for key, value in hashMap.items():
if value % 2 = = 0 :
ans.append(key)
for i in range ( len (ans)):
print (ans[i], end = ' ' )
a = [ 1 , 1 , 1 , 3 , 1 , 2 , 8 , 2 ]
findElements(a)
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static void FindDuplicates( int [] a, int n) {
Dictionary< int , int > dict = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (dict.ContainsKey(a[i])) {
dict[a[i]]++;
} else {
dict[a[i]] = 1;
}
}
List< int > reqdAns = new List< int >();
foreach (KeyValuePair< int , int > entry in dict) {
if (entry.Value % 2 == 0) {
reqdAns.Add(entry.Key);
}
}
for ( int i = 0; i < reqdAns.Count; i++) {
Console.Write(reqdAns[i] + " " );
}
}
public static void Main( string [] args) {
int [] a = { 1, 1, 1, 3, 1, 2, 8, 2 };
FindDuplicates(a, 8);
}
}
|
Javascript
function findElements(a) {
let hashMap = {};
for (let i = 0; i < a.length; i++) {
if (a[i] in hashMap) {
hashMap[a[i]] += 1;
} else {
hashMap[a[i]] = 1;
}
}
let ans = [];
for (let [key, value] of Object.entries(hashMap)) {
if (value % 2 === 0) {
ans.push(parseInt(key));
}
} temp = "" ;
for (let i = 0; i < ans.length; i++) {
temp = temp + ans[i] + " " ;
} console.log(temp);
}
let a = [1, 1, 1, 3, 1, 2, 8, 2];
findElements(a);
|
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N)
Efficient Approach: We can also solve this problem in constant space with the help of approach mentioned in solution 1 of article “Find duplicates in O(n) time and O(1) extra space“
Follow the below steps to solve the problem:
- Traverse the array arr[] and mark the array elements:
arr[abs(arr[i]) -1] = – arr[abs(arr[i]) -1]
- Mark the array elements as positive or negative.
- Use of two pointer approach where pointer i is used to traverse the array and another pointer index points to the end of the returning an array.
- Initially, i and index are initialized to 0.
- abs(arr[i]) appears even times if arr[abs(arr[i] -1]) is positive. Store that value at arr[index] and preserve the sign of value at index then, Increment the index
- Mark arr[abs(arr[i] -1]) as negative, because for the next time if the same value as arr[i] appears, it will be treated as an element appeared even times and also omitted.
- Finally, change every element in the array to +ve in order to get the original values.
- In the end, the index becomes the size of the returning array and the input array itself becomes the returning an array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findDuplicates( int a[], int n)
{
int i, index = 0, sign, returnSize;
for (i = 0; i < n; i++)
a[ abs (a[i]) - 1] = -a[ abs (a[i]) - 1];
for (i = 0; i < n; i++)
{
if (a[ abs (a[i]) - 1] > 0)
{
a[ abs (a[i]) - 1] = -a[ abs (a[i]) - 1];
sign = 1;
if (a[index] < 0)
sign = -1;
a[index++] = sign * abs (a[i]);
}
}
for (i = 0; i < index; i++)
a[i] = abs (a[i]);
returnSize = index;
for (i = 0; i < returnSize; i++)
cout << a[i] << ", " ;
}
int main()
{
int a[] = { 1, 1, 1, 3, 1, 2, 8, 2 };
findDuplicates(a, 8);
}
|
Java
import java.io.*;
class Duplicates {
static void findDuplicates( int a[], int n)
{
int i, index = 0 , sign, returnSize;
for (i = 0 ; i < n; i++)
a[(Math.abs(a[i]) - 1 )]
= -a[(Math.abs(a[i]) - 1 )];
for (i = 0 ; i < n; i++) {
if (a[(Math.abs(a[i]) - 1 )] > 0 ) {
a[(Math.abs(a[i]) - 1 )]
= -a[(Math.abs(a[i]) - 1 )];
sign = 1 ;
if (a[index] < 0 )
sign = - 1 ;
a[index++]
= sign * Math.abs(a[i]);
}
}
for (i = 0 ; i < index; i++)
a[i] = Math.abs(a[i]);
returnSize = index;
for (i = 0 ; i < returnSize; i++)
System.out.print(a[i] + ", " );
}
public static void main(String[] args)
{
int a[] = { 1 , 1 , 1 , 3 , 1 , 2 , 8 , 2 };
findDuplicates(a, 8 );
}
}
|
Python3
def findDuplicates(a, n):
index = 0
for i in range (n):
a[ abs (a[i]) - 1 ] = - a[ abs (a[i]) - 1 ]
for i in range (n):
if (a[ abs (a[i]) - 1 ] > 0 ):
a[ abs (a[i]) - 1 ] = - a[ abs (a[i]) - 1 ]
sign = 1
if (a[index] < 0 ):
sign = - 1
a[index] = sign * abs (a[i])
index + = 1
for i in range (index):
a[i] = abs (a[i])
returnSize = index
for i in range (returnSize):
print (a[i], end = ", " )
a = [ 1 , 1 , 1 , 3 , 1 , 2 , 8 , 2 ]
findDuplicates(a, 8 )
|
C#
using System;
class Duplicates{
static void findDuplicates( int [] a, int n)
{
int i, index = 0, sign, returnSize;
for (i = 0; i < n; i++)
a[( int )(Math.Abs(a[i])) - 1] =
-a[( int )(Math.Abs(a[i])) - 1];
for (i = 0; i < n; i++)
{
if (a[(Math.Abs(a[i]) - 1)] > 0)
{
a[(Math.Abs(a[i]) - 1)] =
-a[(Math.Abs(a[i]) - 1)];
sign = 1;
if (a[index] < 0)
sign = -1;
a[index++] = sign * Math.Abs(a[i]);
}
}
for (i = 0; i < index; i++)
a[i] = Math.Abs(a[i]);
returnSize = index;
for (i = 0; i < returnSize; i++)
Console.Write(a[i] + ", " );
}
public static void Main(String[] args)
{
int [] a = {1, 1, 1, 3, 1, 2, 8, 2};
findDuplicates(a, 8);
}
}
|
Javascript
function findDuplicates(number, number)
{
var number;
var index = 0;
var sign;
var number;
for (i = 0; i < number; i++)
{
number[(Math.abs(number[i]) - 1)] = -number[(Math.abs(number[i]) - 1)];
}
for (i = 0; i < number; i++)
{
if (number[(Math.abs(number[i]) - 1)] > 0)
{
number[(Math.abs(number[i]) - 1)] = -number[(Math.abs(number[i]) - 1)];
sign = 1;
if (number[index] < 0)
{
sign = -1;
}
number[index++] = sign * Math.abs(number[i]);
}
}
for (i = 0; i < index; i++)
{
number[i] = Math.abs(number[i]);
}
var returnSize = index;
for (i = 0; i < returnSize; i++)
{
console.log(number[i] + ", " );
}
}
var number = [1, 1, 1, 3, 1, 2, 8, 2];
findDuplicates(number, 8);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...