Array ( [0] => [1] => questions [2] => Arrays [3] => Odd-occurring ) Arrays | Odd occurring | THE INQUISITIVE





Odd occurring

LEVEL:Beginner

Description

Given an array of integers. You have to find the elements which occurred odd number of times. There should be at least one occurrence in the given array.

Input Format

First line contains the size of the array. Next line contains the n integers separated by the spaces.

Output Format

Elements whose occurred in odd number of times


Example 1:

Input
5
1 1 1 1 1
Output
1
Example 2:

Input
3
1 2 2
Output
1
Example 3:

Input
6
2 5 5 4 1 3
Output
2 4 1 3

oops

Login to see Discussion




Approach

Approach1: Using HashMap

Step-i) Initialize a empty Hashmap and an ArrayList.
Step-ii) Iterate the array and take every element as a.
Step-iii) If a is not present in the hashmap, intialize a in hashmap as 0
Step-iv) If a is already present increase it’s value by 1.
Step-v) Now the hash map is of frequency of every element.
Step-vi) Iterate the hashmap and check the value of every element is odd or not.
Step-vii) If it is odd, add into the array list.
Step-ix) Return the ArrayList.

Time Complexity: O(N)
Space Complexity: O(N)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution