LeetCode Single Number Solution Explained - Java

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • 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
  • Věda a technologie

Komentáře • 34

  • @phungtruong6698
    @phungtruong6698 Před 4 lety +10

    This is the first time i use XOR when coding. It's easy, thank you Nick

  • @NicoleVardo
    @NicoleVardo Před 4 lety +39

    Not stupid at all! I really like this explanation with xor Better than the hashmaps. Thank you

    • @shalsteven
      @shalsteven Před 2 lety

      hasmap need O(n) space, it is forbidden

  • @jankidepala
    @jankidepala Před 4 lety +12

    Its been 4 hours I am trying to figure out XOR...Thanks :)

  • @harinijeyaraman8789
    @harinijeyaraman8789 Před 4 lety +3

    Love your videos man ! Good and concise explanations !

  • @fedest
    @fedest Před 2 lety +4

    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.

  • @nihak309
    @nihak309 Před 2 lety +1

    Explanations helps breaking down the cloud of confusions thanks to you! :)

  • @Gideon_Judges6
    @Gideon_Judges6 Před 4 lety +11

    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 Před 4 lety +1

      the problem description did say that, which is the give-away that XOR is the solution

    • @shriyamsharma2082
      @shriyamsharma2082 Před 4 lety

      Exactly, because if we pass [4,4,1,4] as an array so the program fails!

    • @uHnodnarB
      @uHnodnarB Před 3 lety +1

      @@shriyamsharma2082 the question pretty explicitly says that all numbers that occur more than once occur twice. So yes, that is expected behavior.

  • @ankuraagarwal
    @ankuraagarwal Před 3 lety

    Thanks you Nick! Great job.

  • @olenaqwerty7895
    @olenaqwerty7895 Před 2 lety

    thanks for the video. you are the only person on youtube with so many leet code problem solutions

  • @Mrfuckyounigabitch
    @Mrfuckyounigabitch Před 2 lety

    This is so simple! Thank you!

  • @mashak3765
    @mashak3765 Před 3 lety

    thank you for your explanation - really helpful!

  • @treyquattro
    @treyquattro Před 4 lety +5

    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) :)

  • @yergalemteferi9557
    @yergalemteferi9557 Před rokem

    You did a good explanation and it is very clear thanks

  • @jeffery821217
    @jeffery821217 Před 4 lety +3

    you can show with this: 0^2^2, basically xor twice it's back to 0

  • @tanyongtyy7963
    @tanyongtyy7963 Před 3 lety

    This is smart. Thanks!

  • @niiazbekmamasaliev9828

    XOR makes sense!. good job!

  • @freesoul2677
    @freesoul2677 Před 3 lety

    Thank you!

  • @jay-cover120
    @jay-cover120 Před 3 lety +2

    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 Před 2 lety

      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.

  • @artsysoul1050
    @artsysoul1050 Před 2 lety

    really good explanation

  • @apollossevere8602
    @apollossevere8602 Před 2 lety

    This was Dope!!

  • @souravsaha933
    @souravsaha933 Před rokem

    Very nice 👍🏻

  • @jeezradz
    @jeezradz Před 4 lety

    thanks for the explanation!! not stupid at all.

  • @saketpanchori6480
    @saketpanchori6480 Před 2 lety

    To understand XOR just convert numbers into base 2(0 and 1) then try you will get the unique number always

  • @sukantadasgupta9325
    @sukantadasgupta9325 Před 3 lety

    Thank you... not stupid at all

  • @3thanGamer
    @3thanGamer Před 2 lety

    can somebody explain what the ^= operator does?

  • @sol5179
    @sol5179 Před 4 lety +3

    I don't know why first solution uses less memory...

  • @rishabhjain4546
    @rishabhjain4546 Před 3 lety

    Anything stupid enough to solve this question is not stupid at all !

  • @SK-yb7bx
    @SK-yb7bx Před 3 lety

    Nor