Find all Duplicates in an Array (LeetCode 442) | Full solution with no Extra Space

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 26. 07. 2024
  • You are given an array of integers that have some values that appear twice and others appear once. Return a list of all the duplicate elements. Watch this video to learn 3 methods of solving this question. A Brute Force method, using a Hash-set with extra space and a trick to where you do not need any extra space. The biggest hint is that all the elements of the array are greater than equal to 1 and less than equal to the length of the array. Learn with great animations and visuals of what happens behind the scenes.
    00:00 - Intro
    01:19 - Problem Statement and description
    03:24 - Method 1: Using HashSet
    06:07 - Method 2: Without Extra Space
    12:25 - Dry-run of Code
    14:53 - Final Thoughts
    📚 Links to topics I talk about in the video:
    Brute Force Algorithm: ‱ Brute Force algorithms...
    Time Complexity of an Algorithm: ‱ What is the Time Compl...
    Actual problem on LeetCode: leetcode.com/problems/find-al...
    📘 A text based explanation is available at: studyalgorithms.com/array/lee...
    Code on Github: github.com/nikoo28/java-solut...
    Test-cases on Github: github.com/nikoo28/java-solut...
    📖 Reference Books:
    Starting Learn to Code: amzn.to/36pU0JO
    Favorite book to learn algorithms: amzn.to/39w3YLS
    Favorite book for data structures: amzn.to/3oAVBTk
    Get started for interview preparation: amzn.to/39ysbkJ
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    đŸŽ„ My Recording Gear:
    Recording Light: amzn.to/3pAqh8O
    Microphone: amzn.to/2MCX7qU
    Recording Camera: amzn.to/3alg9Ky
    Tablet to sketch and draw: amzn.to/3pM6Bi4
    đŸ’» Get Social đŸ’»
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #interview

Komentáƙe • 44

  • @abhi121able
    @abhi121able Pƙed 2 lety +2

    Superb Work. Explanation is too good. Very smooth :)

  • @ranchodas
    @ranchodas Pƙed 5 měsĂ­ci +2

    Very well explained bro !!

  • @gauravmasalkar3546
    @gauravmasalkar3546 Pƙed rokem +1

    great work keep it doing

  • @shraddhajain8935
    @shraddhajain8935 Pƙed 3 lety

    Really helpful. Thanks

  • @deepeshreddy4700
    @deepeshreddy4700 Pƙed 3 lety +10

    Please don't stop uploading videos it is helping a lot for students like me

    • @nikoo28
      @nikoo28  Pƙed 3 lety

      Sure...do help me out by sharing this video as much as you can :)

    • @deepeshreddy4700
      @deepeshreddy4700 Pƙed 3 lety

      @@nikoo28 Okay

  • @deliveringIdeas
    @deliveringIdeas Pƙed rokem

    Thank you tremendously

  • @rishidangi2978
    @rishidangi2978 Pƙed 4 měsĂ­ci +1

    this one and yesterday's problem where we assumed our array to be a linked list and tried to find the node where the loop starts (Leetcode 287: find the duplicate number) were kinda hard to think when solving for the first time.
    One thing I can surely note from this that if the value of integers is always less than the length of the array (or one less), we can use it as an index to the array.
    Thanks for the explanation!!

    • @nikoo28
      @nikoo28  Pƙed 3 měsĂ­ci

      What was the programming contest you are referring to?

  • @user-mx9kp8js8v
    @user-mx9kp8js8v Pƙed 4 měsĂ­ci

    great explanation

  • @NikhilVerma-kk6xc
    @NikhilVerma-kk6xc Pƙed 10 měsĂ­ci

    bhaisahab mst explaination

  • @shivaniverma4266
    @shivaniverma4266 Pƙed 10 měsĂ­ci +1

    you are genus you are amazing you just smoothly put that logic in hour mind that in my dreams i can scream it lout all i am saying is whaattttttta content , you have such a great skills keeep doing , i am greatful for this channel

    • @nikoo28
      @nikoo28  Pƙed 9 měsĂ­ci

      thank you so much for these kind and awesome words

  • @pratikpatrimath4887
    @pratikpatrimath4887 Pƙed rokem +1

    superb 😎😎😎

  • @rajkumarp9943
    @rajkumarp9943 Pƙed 10 měsĂ­ci

    Good đŸ’„

  • @utkarshjoshi2915
    @utkarshjoshi2915 Pƙed rokem

    Example taken in method 2, what if second occurrence of 3 is before 2(i.e, 2 and 3 change their positions of second occurrence, 2 at index 6 and 3 at index 5), at that situation , answer will not be in ascending order. Then we have to sort the returning array again ?

  • @PRASANTH-if9se
    @PRASANTH-if9se Pƙed 9 měsĂ­ci +1

    why answer is repeating when they occur in the array more than 4 times

  • @chinmayjain9705
    @chinmayjain9705 Pƙed 2 lety

    Heap sort can be use with space comp O(n).
    If 0(1)
    Arr[arr[i]%n ] = +n
    New arr[I]
    Again we will bring back original array by
    Arr[I]

  • @AmitChauhan-sp1cw
    @AmitChauhan-sp1cw Pƙed rokem +1

    how to implement code for returning -1 when repeating element does not exist?

    • @nikoo28
      @nikoo28  Pƙed rokem +1

      That will be a very small change in the code. If the resultSet is empty at the end, you can return a “-1”

  • @choudharyhimanshu4134
    @choudharyhimanshu4134 Pƙed rokem

    Can't we first sort the array and then iterate over the array and look for duplicates by comparing adjacent elements. Sorting will take O(nlogn) time and iteration will take O(n) time and no extra space is required. So, overall TC is O(n).
    Do correct me if I am wrong.

  • @manishkumar-xc2zp
    @manishkumar-xc2zp Pƙed rokem +2

    why we have (index+1) , can anyone explain

    • @nikoo28
      @nikoo28  Pƙed rokem

      That is because the index ranges from 0 to (n-1)
      But the array elements are from 1 to n
      So if you do (index + 1) you are referring to the actual elements present in the array.

  • @chinmayjain9705
    @chinmayjain9705 Pƙed 2 lety

    👍

  • @sreddy8141
    @sreddy8141 Pƙed 3 lety

    Bhai, wanna know about desktop space analysis problem

    • @nikoo28
      @nikoo28  Pƙed 3 lety +1

      I can’t find the link to the problem. Reach out to me on my email available in channel information.

  • @vanshbhatnagar1117
    @vanshbhatnagar1117 Pƙed 10 měsĂ­ci

    class Solution {
    public:
    vector findDuplicates(vector& nums) {
    vector result ;
    for(int i=0; i

  • @user-oz2eu7rs8v
    @user-oz2eu7rs8v Pƙed 4 měsĂ­ci +2

    don't you think you are using o(n) space by modifying the array itself

    • @unemployedcse3514
      @unemployedcse3514 Pƙed 2 měsĂ­ci +1

      space remains constant since , seperate or extra space is not utilised

  • @hemav9981
    @hemav9981 Pƙed 7 měsĂ­ci

    If the element is 0 ,index becomes -1. It is showing array index out of bound error. How to solve this issue?

  • @prabaljana9714
    @prabaljana9714 Pƙed 10 měsĂ­ci

    If the element encounted 0 the. What to do

    • @nikoo28
      @nikoo28  Pƙed 9 měsĂ­ci

      Then you will have to handle the 0 differently. For this particular problem check the constraints: 1

  • @AkashRK07
    @AkashRK07 Pƙed 8 měsĂ­ci +1

    Doesn't creating a result set consider as extra space?

    • @nikoo28
      @nikoo28  Pƙed 7 měsĂ­ci +2

      You are expected to return a result set, no need to count that.
      When we talk about space complexity, usually it is the extra space you would be needing.

    • @AkashRK07
      @AkashRK07 Pƙed 7 měsĂ­ci

      @@nikoo28 understood đŸ™ŒđŸ»

  • @suryav8458
    @suryav8458 Pƙed 10 měsĂ­ci

    Please some times name the Alogrithm name also
    otherwise everything is good

    • @nikoo28
      @nikoo28  Pƙed 9 měsĂ­ci +1

      not every algorithm has a name, and your interviewer will never ask the algorithm name.

  • @nikhilbansal3731
    @nikhilbansal3731 Pƙed 6 měsĂ­ci

    What is name of the approach

    • @nikoo28
      @nikoo28  Pƙed 6 měsĂ­ci

      I don't know if there is a specific name to this approach

  • @ujjawalsharma1120
    @ujjawalsharma1120 Pƙed rokem

    it will not give sorted array for this input n = 8,
    4 3 2 4 8 2 3 1, how to fix it, pls correct me if input is not valid.

    • @nikoo28
      @nikoo28  Pƙed rokem

      What do you mean by not giving a sorted array? You are looking for duplicates..right??