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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the elements // in the array having even frequency void findDuplicates( int a[], int n) { // Declaring hashmap to store count og elements 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); } // printing required elements that are satisfying the // conditions for ( int i = 0; i < reqdAns.size(); i++) cout << reqdAns[i] << " " ; } // Driver code int main() { int a[] = { 1, 1, 1, 3, 1, 2, 8, 2 }; findDuplicates(a, 8); } // This code is contributed by divyansh2212 |
Java
// Java program to implement // the above approach import java.util.*; public class Main { // Function to find the elements // in the array having even frequency static void findDuplicates( int [] a, int n) { // Declaring hashmap to store count of elements 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()); } // printing required elements that are satisfying the // conditions for ( int i = 0 ; i < reqdAns.size(); i++) System.out.print(reqdAns.get(i) + " " ); } // Driver code public static void main(String[] args) { int [] a = { 1 , 1 , 1 , 3 , 1 , 2 , 8 , 2 }; findDuplicates(a, 8 ); } } // This code is contributed by Aman Kumar. |
Python3
# python code to find the elements occurring even number of times def findElements(a): hashMap = {} # defining a hashmap here a dictionary # adding values and their frequency to the map for i in range ( len (a)): if a[i] in hashMap: hashMap[a[i]] + = 1 else : hashMap[a[i]] = 1 # initializing the output array ans = [] # adding values to the output array 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 ] # calling the function findElements(a) # code contributed by gurkirat2600 |
C#
// C# program to implement the above approach using System; using System.Collections.Generic; class MainClass { // Function to find the elements // in the array having even frequency static void FindDuplicates( int [] a, int n) { // Declaring dictionary to store count of elements 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); } } // printing required elements that are satisfying // the conditions for ( int i = 0; i < reqdAns.Count; i++) { Console.Write(reqdAns[i] + " " ); } } // Driver code public static void Main( string [] args) { int [] a = { 1, 1, 1, 3, 1, 2, 8, 2 }; FindDuplicates(a, 8); } } // This code is contributed by Utkarsh Kumar |
Javascript
// JavaScript code to find the elements occurring even number of times function findElements(a) { let hashMap = {}; // defining a hashmap here a dictionary // adding values and their frequency to the map for (let i = 0; i < a.length; i++) { if (a[i] in hashMap) { hashMap[a[i]] += 1; } else { hashMap[a[i]] = 1; } } // initializing the output array let ans = []; // adding values to the output array 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]; // calling the function findElements(a); |
1 2
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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the elements // in the array having even frequency 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[i] has occurred even times if (a[ abs (a[i]) - 1] > 0) { // Mark a[abs(a[i])-1] a[ abs (a[i]) - 1] = -a[ abs (a[i]) - 1]; // Preserve the sign of a[index] sign = 1; if (a[index] < 0) sign = -1; a[index++] = sign * abs (a[i]); } } // Make every array element +ve for (i = 0; i < index; i++) a[i] = abs (a[i]); returnSize = index; for (i = 0; i < returnSize; i++) cout << a[i] << ", " ; } // Driver code int main() { int a[] = { 1, 1, 1, 3, 1, 2, 8, 2 }; findDuplicates(a, 8); } // This code is contributed by sanjoy_62 |
Java
// Java Program to implement // the above approach import java.io.*; class Duplicates { // Function to find the elements // in the array having even frequency 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[i] has occurred even times if (a[(Math.abs(a[i]) - 1 )] > 0 ) { // Mark a[abs(a[i])-1] a[(Math.abs(a[i]) - 1 )] = -a[(Math.abs(a[i]) - 1 )]; // Preserve the sign of a[index] sign = 1 ; if (a[index] < 0 ) sign = - 1 ; a[index++] = sign * Math.abs(a[i]); } } // Make every array element +ve 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] + ", " ); } // Driver code public static void main(String[] args) { int a[] = { 1 , 1 , 1 , 3 , 1 , 2 , 8 , 2 }; findDuplicates(a, 8 ); } } |
Python3
# Python3 program to implement # the above approach # Function to find the elements # in the array having even frequency 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[i] has occurred even times if (a[ abs (a[i]) - 1 ] > 0 ): # Mark a[abs(a[i])-1] a[ abs (a[i]) - 1 ] = - a[ abs (a[i]) - 1 ] # Preserve the sign of a[index] sign = 1 if (a[index] < 0 ): sign = - 1 a[index] = sign * abs (a[i]) index + = 1 # Make every array element +ve for i in range (index): a[i] = abs (a[i]) returnSize = index for i in range (returnSize): print (a[i], end = ", " ) # Driver code a = [ 1 , 1 , 1 , 3 , 1 , 2 , 8 , 2 ] findDuplicates(a, 8 ) # This code is contributed by code_hunt |
C#
// C# Program to implement // the above approach using System; class Duplicates{ // Function to find the elements // in the array having even frequency 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[i] has occurred even times if (a[(Math.Abs(a[i]) - 1)] > 0) { // Mark a[abs(a[i])-1] a[(Math.Abs(a[i]) - 1)] = -a[(Math.Abs(a[i]) - 1)]; // Preserve the sign of a[index] sign = 1; if (a[index] < 0) sign = -1; a[index++] = sign * Math.Abs(a[i]); } } // Make every array element +ve for (i = 0; i < index; i++) a[i] = Math.Abs(a[i]); returnSize = index; for (i = 0; i < returnSize; i++) Console.Write(a[i] + ", " ); } // Driver code public static void Main(String[] args) { int [] a = {1, 1, 1, 3, 1, 2, 8, 2}; findDuplicates(a, 8); } } // This code is contributed by shikhasingrajput |
Javascript
// Function to find the elements // in the array having even frequency 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 a[i] has occurred even times if (number[(Math.abs(number[i]) - 1)] > 0) { // Mark a[abs(a[i])-1] number[(Math.abs(number[i]) - 1)] = -number[(Math.abs(number[i]) - 1)]; // Preserve the sign of a[index] sign = 1; if (number[index] < 0) { sign = -1; } number[index++] = sign * Math.abs(number[i]); } } // Make every array element +ve 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] + ", " ); } } // Driver code var number = [1, 1, 1, 3, 1, 2, 8, 2]; findDuplicates(number, 8); // This code is contributed by sourabhdalala0001. |
1, 2,
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...