Solving LeetCode 136 in JavaScript (Single Number)

preview_player
Показать описание
In this video I solve LeetCode problem 136 (Single Number) with the JavaScript programming language. This question has previously appeared during Amazon coding interviews.

I start by helping you understand what the problem is asking for, then I will go over the pseudo-code/logic for a solution, and finally I will write the solution code explaining each line, step-by-step.

Using my simple and easy-to-understand methodology, I make difficult and complex interview questions *incredibly* easy to understand. So easy in fact, that even your grandma could do it! 👵🏻

I will be solving a new problem EVERY WEEK, so be sure to smash that subscribe button and hit the notification bell so you don't miss a single one! 😀

-----
I also offer full-length interview preparation courses, programming tutorials, and even 1-1 tutoring!

Who knew acing your next software engineering interview could be *so* incredibly easy!

-----
#programming #codinginterview #leetcode
Рекомендации по теме
Комментарии
Автор

You must implement a solution with a linear runtime complexity and use only constant extra space.

somebody-
Автор

If in case anyone is interested, the O(1) space complexity requires some bit manipulation. Essentially if you XOR a number with itself, it returns zero, and if you XOR a number with zero, it returns the number itself.
So if you had the following numbers in the list - 3, 3, 2, the XOR between 3 and 3 would return 0, and then XORing 0 with 2 would return 2. Hence if you did a XOR for all numbers in a list which contains 2 of each numbers except for the one number, then you'd get back the number which occurs once.

The code is pretty straightforward since you only need to do a XOR for every number, and return the result.
Code in Javascript -

var singleNumber = function(nums) {
let res = nums[0];

for (let i = 1; i<nums.length; i++) {
res = res ^ nums[i];
}

return res;
};

suvajitchakrabarty
Автор

it asks it to be in O(1) space complexity. How would you adapt this for that

BabeCrs
Автор

ht[num] = ht[num] +1 || 1 can you please explain more on this part?

jinbo
Автор

isn't const ht an Object? When should we use map or object?

feng
join shbcf.ru