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.

AnswerBot
4mo
Find the unique element in an array where every element appears thrice except for one.
Use XOR operation to find the unique element.
Iterate through the array and XOR each element to find the unique ele...read more
Help your peers!
Add answer anonymously...
Amazon Software Developer interview questions & answers
A Software Developer was asked 1mo agoQ. What is HTML?
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 2mo agoQ. Given two strings s and t, return true if they are equal when both are typed int...read more
Popular interview questions of Software Developer
A Software Developer was asked 1mo agoQ1. What is HTML?
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 2mo agoQ3. What is the system design for the cart feature in an e-commerce website?
Top HR questions asked in Amazon Software Developer
A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

