Contains Duplicate (LeetCode 217) | Full solution with HashSet explanation | Study Algorithms

Поділитися
Вставка
  • Опубліковано 20 жов 2024

КОМЕНТАРІ • 30

  • @SanthoshaK-px8rq
    @SanthoshaK-px8rq 3 місяці тому +3

    Another Method that we can sort the array and check its neighbor values.
    Arrays.sort(nums);
    for(int i=1;i

    • @bhavitvora9364
      @bhavitvora9364 10 днів тому

      Due to sorting, the time complexity will be O(nlogn). And using a HashSet will do it in O(n)

  • @pavanrajjagdale9369
    @pavanrajjagdale9369 2 роки тому +4

    Really Great explanation👍.Thank you 😊

  • @vineeshreddy9998
    @vineeshreddy9998 9 місяців тому +1

    Superb explanation. Thank you

  • @viniz0r
    @viniz0r 6 місяців тому

    such a great explanation, thank you mate

  • @satyamgupta6030
    @satyamgupta6030 Рік тому +1

    thank you so much bhaiya for such a great explaination.

  • @pranaym143
    @pranaym143 2 роки тому

    Simple and lucid explaination

  • @vamshisundupalle6141
    @vamshisundupalle6141 Рік тому

    Your explanation is so good

  • @mohammedilyas8824
    @mohammedilyas8824 2 роки тому +2

    Great explanation and amazing content sir

    • @nikoo28
      @nikoo28  2 роки тому +1

      Glad I could help :)

  • @kadirv
    @kadirv 6 місяців тому

    Sorry, as for the time-complexity - you check for the element in hash-set. How exactly does this happen? Won't all the has set elements be checked against to check if a number is in there or not? then the time complexity will be n square?

    • @nikoo28
      @nikoo28  5 місяців тому

      Checking in HashSet happens in O(1) time

  • @pentainamerica
    @pentainamerica 6 місяців тому

    isn't the time complexity n logn because it takes n to traverse through the array and for each element it might take log n to insert in hash set?
    or am I confusing with hash map?

    • @nikoo28
      @nikoo28  5 місяців тому

      Hashset insert takes place in O(1)

  • @lakshaysaini2070
    @lakshaysaini2070 5 місяців тому

    i have a doubt. the contains method of hashset also has a time complexity of O(n) and then there is for loop as well so shouldnt the time complexity be O(n^2). Is it the same as brute force approach?

    • @nikoo28
      @nikoo28  5 місяців тому

      Contains method works in O(1)

    • @lakshaysaini2070
      @lakshaysaini2070 5 місяців тому

      @nikoo28 thanks for replying back. But how is it O(1) cuz it will search through the whole set right?

  • @SatishSingh-eb5tq
    @SatishSingh-eb5tq 5 місяців тому

    We could have avoided the contains check. add() will return false if the number is already present!!

  • @anshumansolanki6775
    @anshumansolanki6775 Рік тому

    Why are we using a HashSet and not a HashMap Nikhil ??

    • @nikoo28
      @nikoo28  Рік тому

      what advantage would a hashmap give you?

    • @anshumansolanki6775
      @anshumansolanki6775 Рік тому +1

      @@nikoo28 you are right we cant say it will improve the algorithm, can we say its one of the solutions? If I find the entry in map I can say there is a duplicate

  • @jintodo6550
    @jintodo6550 Рік тому

    very nice

  • @abhinavd2
    @abhinavd2 Рік тому

    Super ❤️

  • @rajabhowmick1342
    @rajabhowmick1342 7 місяців тому

    for (int n : nums) {
    if(!set.add(n)) {
    return true;
    }
    }

  • @karthik-varma-1579
    @karthik-varma-1579 6 місяців тому

    GREAT

  • @SushmaSulthan
    @SushmaSulthan 7 місяців тому

    Bro it is tricky in c

    • @nikoo28
      @nikoo28  7 місяців тому

      what part are you facing a problem with?

  • @himanshujakhmola5960
    @himanshujakhmola5960 6 місяців тому

    simply explain ..