Two Non-Repeating Elements

Given an array 'ARR' of integers of size N in which two elements appear exactly once and all other elements appear exactly twice. Your task is to find the two elements that appear only once.

Note
 1. Input will always have only two integers that appear exactly once rest will appear twice.
 2. Try to do this in linear time and constant space.
Input Format:
The first line of input contains an integer N representing the size of the array.

The second line of input contains an N space-separated integers representing the array elements.
Output Format:
Print the two space-separated elements that appear only once where the first integer is less than the second integer.
Constraints:
2 <= N <= 10^5
-10^5 <= ARR[i] <= 10^5

Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force
  • We will maintain a list or vector to store the two elements that appear only once.
  • We will run a loop from i = 0 to i = N - 1 and for i’th loop, we need to check if there is another element ...read more
CodingNinjas
author
2y
Sorting

We will sort the array and run a loop from i = 0 to i = N - 1 and for i’th element check its adjacent element if any one of the adjacents is same then skip this element otherwise we can print t...read more

CodingNinjas
author
2y
Mapping
  • We will be using a hashmap and increase the count for each element in the hashmap.
  • Again iterate in the hashmap and if the occurrence is 1 then add it to the list.
  • Now print the answer as require...read more
CodingNinjas
author
2y
Bit masking
  • First of all, we will find the xor of all the elements since every element occurs twice except the two elements which occur once. So we will have the xor of those two elements let’s call it...read more
Add answer anonymously...
TO THE NEW Technical Trainee 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