LeetCode Single Number Solution Explained - Java

  Рет қаралды 30,348

Nick White

Nick White

Күн бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow My Twitter - / nicholaswwhite
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 34
@NicoleVardo
@NicoleVardo 4 жыл бұрын
Not stupid at all! I really like this explanation with xor Better than the hashmaps. Thank you
@shalsteven
@shalsteven 2 жыл бұрын
hasmap need O(n) space, it is forbidden
@phungtruong6698
@phungtruong6698 4 жыл бұрын
This is the first time i use XOR when coding. It's easy, thank you Nick
@jankidepala
@jankidepala 4 жыл бұрын
Its been 4 hours I am trying to figure out XOR...Thanks :)
@harinijeyaraman8789
@harinijeyaraman8789 4 жыл бұрын
Love your videos man ! Good and concise explanations !
@nihak309
@nihak309 2 жыл бұрын
Explanations helps breaking down the cloud of confusions thanks to you! :)
@olenaqwerty7895
@olenaqwerty7895 2 жыл бұрын
thanks for the video. you are the only person on youtube with so many leet code problem solutions
@Gideon_Judges6
@Gideon_Judges6 4 жыл бұрын
Keep in mind that this solution only works if you have even numbers of duplicates (e.g. pairs as in the problem statement). As soon as you have odd number of duplicates it fails. Note that XOR is the binary operator of the more general n-ary odd function.
@treyquattro
@treyquattro 4 жыл бұрын
the problem description did say that, which is the give-away that XOR is the solution
@shriyamsharma2082
@shriyamsharma2082 4 жыл бұрын
Exactly, because if we pass [4,4,1,4] as an array so the program fails!
@uHnodnarB
@uHnodnarB 3 жыл бұрын
@@shriyamsharma2082 the question pretty explicitly says that all numbers that occur more than once occur twice. So yes, that is expected behavior.
@fedest
@fedest 2 жыл бұрын
Here’s my explanation on why it works, regardless of the order of the numbers: Keep in mind that for any bit A, you have A^0 == A, and A^1 != A, or said differently, xor’ing with a 1 “flips” its value, and xor’ing with a 0 leaves it unchanged. Now consider each individual bit position in the int result. As you traverse the array, you’ll see ints having either a 0 or 1 in that position. The total number of 1s seen for that position, indicates how many times the bit at that position in result is flipped. Of course, for duplicate numbers, you’ll see the same bit twice, and flipping a bit twice (or any even number of times) is the same as leaving it unchanged. Since result starts with all the bits as 0, in the end the value for the bit at each position will be 1 iff the total number of 1s at that position is odd, and that can only happen if the unique number in the array has a 1 at that bit position. Therefore, all bits in result will have the same value as the bits in the unique number, so they are the same number.
@Mrfuckyounigabitch
@Mrfuckyounigabitch 2 жыл бұрын
This is so simple! Thank you!
@ankuraagarwal
@ankuraagarwal 3 жыл бұрын
Thanks you Nick! Great job.
@mashak3765
@mashak3765 3 жыл бұрын
thank you for your explanation - really helpful!
@yergalemteferi9557
@yergalemteferi9557 Жыл бұрын
You did a good explanation and it is very clear thanks
@niiazbekmamasaliev9828
@niiazbekmamasaliev9828 2 жыл бұрын
XOR makes sense!. good job!
@treyquattro
@treyquattro 4 жыл бұрын
nicely explained. Any number XOR'd with itself is zero. Any number XOR'd with zero is the number itself. It really helps to know binary representations of numbers, and also assembler-level programming (although not strictly necessary) :)
@jeffery821217
@jeffery821217 4 жыл бұрын
you can show with this: 0^2^2, basically xor twice it's back to 0
@artsysoul1050
@artsysoul1050 2 жыл бұрын
really good explanation
@jay-cover120
@jay-cover120 3 жыл бұрын
a good way to think of this is that there's only one distinct element in the list, every other pair of duplicate elements are going to be 0 if doing the XOR operation. so the whole for loop is basically reduced to just doing one XOR operation, which is 0 XOR that distinct element. And that is why the initial value set to 0.
@vanraj8169
@vanraj8169 2 жыл бұрын
But the process is not that way!! You can do it by performing every element sequentially. That's why he said, concept behind this is bit tricky.
@tanyongtyy7963
@tanyongtyy7963 3 жыл бұрын
This is smart. Thanks!
@freesoul2677
@freesoul2677 3 жыл бұрын
Thank you!
@souravsaha933
@souravsaha933 Жыл бұрын
Very nice 👍🏻
@apollossevere8602
@apollossevere8602 2 жыл бұрын
This was Dope!!
@jeezradz
@jeezradz 4 жыл бұрын
thanks for the explanation!! not stupid at all.
@saketpanchori6480
@saketpanchori6480 2 жыл бұрын
To understand XOR just convert numbers into base 2(0 and 1) then try you will get the unique number always
@sukantadasgupta9325
@sukantadasgupta9325 3 жыл бұрын
Thank you... not stupid at all
@3thanGamer
@3thanGamer 2 жыл бұрын
can somebody explain what the ^= operator does?
@sol5179
@sol5179 4 жыл бұрын
I don't know why first solution uses less memory...
@shalsteven
@shalsteven 2 жыл бұрын
which solution?
@rishabhjain4546
@rishabhjain4546 3 жыл бұрын
Anything stupid enough to solve this question is not stupid at all !
@SK-yb7bx
@SK-yb7bx 3 жыл бұрын
Nor
LeetCode Word Search Solution Explained - Java
12:08
Nick White
Рет қаралды 45 М.
LeetCode 442. Find All Duplicates in an Array (Solution Explained)
12:37
LeetCode Backspace String Compare Solution Explained - Java
7:42
Single Number - Leetcode 136 - Python
7:09
NeetCode
Рет қаралды 86 М.
Doing LeetCode Be Like (Coding Interviews Be Like Pt. 2)
4:41
Nicholas T.
Рет қаралды 746 М.
I solved 541 Leetcode problems. But you need only 150.
7:42
Sahil & Sarra
Рет қаралды 2,2 МЛН
LeetCode 67. Add Binary Solution Explained - Java
10:24
Nick White
Рет қаралды 45 М.
LeetCode 3Sum Solution Explained - Java
10:00
Nick White
Рет қаралды 194 М.
I Got Rejected (again)
9:43
Nick White
Рет қаралды 202 М.
Binary Search Algorithm in 100 Seconds
2:20
Fireship
Рет қаралды 529 М.
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 500 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 421 М.
Урна с айфонами!
0:30
По ту сторону Гугла
Рет қаралды 8 МЛН