Unique Element in Array
Given an arbitrary array arr
consisting of N
non-negative integers where every element appears thrice except for one. Your task is to find the element in the array that appears only once.
Input:
First line: Single integer T
indicating the number of test cases.
Next 2 * T
lines: For each test case,
- A single integer N
denoting the number of elements in the array.
- N
space-separated integers representing the elements of the array.
Output:
For each test case, output a single integer — the element that appears only once.
Example:
Input:
T = 2
arr1 = [2, 2, 3, 2]
arr2 = [0, 1, 0, 1, 0, 1, 99]
Output:
3
99
Explanation:
In the first test case, every element appears thrice except for 3
, which appears once. In the second test case, the element 99
appears only once.
Constraints:
1 ≤ T ≤ 100
4 ≤ N ≤ 104
0 ≤ arr[i] < 109
Note:
No need to print anything; just implement the function to return the answer.
Follow Up:
Try to achieve O(N) time complexity and O(1) space complexity.
Be the first one to answer
Add answer anonymously...
Top Amazon Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Amazon Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app