Given an array ‘ARR’ of ‘N’ integers, where all the elements occur an even number of times and only one number occurs an odd number of times. Find the number which occurs an odd number of times.
EXAMPLE:
Input:
'N' = 5
'ARR' = 1 2 3 2 3
Output: 1
Except for number 1 all numbers occur even number of times.
Input Format :
The first line will contain integer 'T', the number of test cases. For each test case, there will be only two lines.
The first line contains a single integer, 'N' representing the size of the array.
The second line contains ‘N’ spaced integers.
Output format :
For each test case, print a single integer representing the number occurring odd number of times.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= 'T' <= 10
1 <= 'N' <= 10^4
1 <= ARR[i] <= 10^5
Time Limit: 1 sec
Calculate the sum of first n natural numbers as sumtotal= n*(n+1)/2
Create a variable sum to store the sum of array elements.
Traverse the array from start to end.
Update the value of sum as sum = sum + ...read more
Approach:
We will run two nested loops and count the occurrence of every element.
If at any moment, we are getting the count as odd, we will just print that element.
Algorithm :
- For i = ...read more
Approach:
We will hash be using a hash data structure, so here we will store the frequency of an element ‘ARR[i]’ at ‘hash[ARR[i]]’.
Each time we encounter a number ARR[i] we will increase the ...read more
Approach:
The bitwise Xor all elements of the array where even occurring elements on Xor will give 0 whereas odd occurring element on xor give us the number since its even occurrence will ...read more
Top Nagarro Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month