Solving LeetCode 136 in JavaScript (Single Number)
Vložit
- čas přidán 18. 11. 2020
- In this video I solve LeetCode problem 136 (Single Number) with the JavaScript programming language. This question has previously appeared during Amazon coding interviews.
▶ Get my FREE Data Structures Crash Course: bit.ly/2YWCn1C
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! 😀
▶ Single Number: leetcode.com/problems/single-...
-----
I also offer full-length interview preparation courses, programming tutorials, and even 1-1 tutoring!
▶ Get access to MY COMPLETE SYSTEM: kaeducation.com
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.
it asks it to be in O(1) space complexity. How would you adapt this for that
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
very useful ❤️
isn't const ht an Object? When should we use map or object?
a hash map is simply an object in JS
@@Moe1993x so when you are asked to build a custom hashmap, should we do const hash = new Map; or const hash = {}?
ht[num] = ht[num] +1 || 1 can you please explain more on this part?
the first time, ht[num] will be undefined. At that time, undefined + 1 will be NaN which is false. Hence ht[num] will be initialized 1 because of || 1. It is explained properly at 2:05
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR