Meta / Amazon / Google / Microsoft| Single Number III | LeetCode-260: Medium | JavaScript Implementation

Manan Kumar Gupta
1 min readMar 27, 2022

We will be seeing the solution to the LeetCode problem 260 in JavaScript. I hope you will find it useful.

Problem Statement:
Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

Link to Problem: https://leetcode.com/problems/single-number-iii/

JavaScript Implementation:

var singleNumber = function(nums) {
let completeXor = 0;
for(let num of nums){
completeXor ^= num;
}
let leftMostSetBit = 1;
while((leftMostSetBit & completeXor) == 0){
leftMostSetBit <<=1
}
let res = [0,0]
for(let num of nums){
if(leftMostSetBit & num){
res[0] ^= num
}else{
res[1] ^= num
}
}
return res
};

Thank you for reading hope this code is helpful to you.

--

--

Manan Kumar Gupta

I am Frontend Developer, exploring the power of Javascript. I have worked on Angular, ReactJS and React Native.