JavaScript Interview Question: Bit Counting

Поделиться
HTML-код
  • Опубликовано: 24 ноя 2024

Комментарии • 5

  • @abdulroufshaik123
    @abdulroufshaik123 Месяц назад +2

    Well explained!!

  • @evilgaming000
    @evilgaming000 Месяц назад +1

    You can directly return the length of the array, don't have to use join to Marge the array and then return the length.

    • @LogicLab_YT
      @LogicLab_YT  Месяц назад +2

      Good point! But if we skip join it won’t work quite right. Here’s why when we do n.toString(2).split('0'), it creates an array of parts separated by '0's... For example, 1234 in binary is 10011010010. Splitting by 0 gives us this [ '1', '', '11', '1', '', '1', '' ]. Now if we take the length here it counts the parts in the array, not just the '1s.
      So join('') is needed to merge it all back together before counting. But great observation.. definitely try it out yourself to see the difference... :)

    • @evilgaming000
      @evilgaming000 29 дней назад +1

      @@LogicLab_YT oh yeah I didn't think about the edge point, you're right they can be 11 or even 111,,, thanks

  • @ritiknandre
    @ritiknandre 28 дней назад +1

    Pr esa krna ku h 😢