Array ( [0] => [1] => questions [2] => Strings [3] => Odd-occurring )
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.
First line contains the size of the array. Next line contains the n integers separated by the spaces.
Elements whose occurred in odd number of times
5 1 1 1 1 1
1
3 1 2 2
1
6 2 5 5 4 1 3
2 4 1 3
Login to see Discussion
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.
Login to see Solution