Element that appears once

You are given an arbitrary array ‘arr’ consisting of N non-negative integers, where every element appears thrice except one. You need to find the element that appears only once.

Input Format:
The first line of the input contains a single integer T, representing the number of test cases. 

The first line of each test case consists of a single integer N, representing the number of elements in the given array.

The second line of each test case contains N space-separated integers, denoting the elements of the array.
Output Format:
For each test case, print a single integer representing the element that appears only once in the array.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function. 
Constraints:
1 <= T <= 100
4 <= N <= 10^4
0 <= arr[i] < 10^9
Time Limit: 1sec
Follow Up:
Try to solve this problem in O(N) time and O(1) space complexity.
CodingNinjas
author
2y
Store the count of all the elements

An easy way to solve this problem is to first store the count of all the elements in a map with keys as elements of the array and value as their frequencies. Then i...read more

CodingNinjas
author
2y
Try to form a mathematical relation to solve this problem
  • We can also solve this problem by formulating a mathematical equation between the actual sum of elements in the array, and the sum of elements ...read more
CodingNinjas
author
2y
Use bit manipulation.
  • We can utilize bit manipulation to solve this problem.
  • For each bit position i, we count the number of elements whose ith bit is set. Let this be ‘count’.
  • For bit positions where (c...read more
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter