Array - 19: Find the first repeating value in array

Sdílet
Vložit
  • čas přidán 17. 09. 2019
  • Code: thecodingsimplified.com/find-...
    Solution:
    - We'll take a Set & a variable firstRepeat
    - We'll iterate from last element & if value doesn't present in Set, put this value in Set.
    - If element already present in Set, update this value with firstRepeat.
    - The last updated value in firstRepeat will contain the repeated value, as we'll movinf from back.
    - Time Complexity: O(n) for iterating the complete array
    - Space Complexity: O(n) for Set
    Please check video for more info:
    This problem is similar to:
    array find first repeating value,
    find first repeating value in array,
    first repeating value,
    array,
    java tutorial,
    coding simplified,
    java
    CHECK OUT CODING SIMPLIFIED
    / codingsimplified
    I started my CZcams channel, Coding Simplified, during Dec of 2015.
    Since then, I've published over 300+ videos. My account is Partner Verified.
    ★☆★ VIEW THE BLOG POST: ★☆★
    codingsimplified.com
    ★☆★ SUBSCRIBE TO ME ON CZcams: ★☆★
    czcams.com/users/codingsimplif...
    ★☆★ SEND EMAIL At: ★☆★
    Email: thecodingsimplified@gmail.com

Komentáře • 13

  • @anjalisingh-sx5ct
    @anjalisingh-sx5ct Před 3 lety +2

    int min= -1;
    //If element already in hash set
    if (myset.find(arr[i]) != myset.end())
    min = i;
    else // Else add element to hash set
    myset.insert(arr[i]);
    This is c++ code i didn't get third line what myset.end( ) points to end of set...then how will it find the repeated number...pls help

  • @YashRaithatha1989
    @YashRaithatha1989 Před 4 lety

    Good work. Keep it up !

  • @nethravathim8422
    @nethravathim8422 Před rokem

    If the array has 2,4,4,5,5,7,7 in this case first occurance is 4 but set will return 7. But the answer has to be 4 because this is the first reperating occurance in the array. Explain me this if i am wrong

  • @rajasakthikumarrajendran5806

    can we do it with linked hashmap

  • @sourav5562
    @sourav5562 Před 4 lety

    I do not understand what will happen if we begin from starting. and if we take Tree set? explain plz

    • @CodingSimplified
      @CodingSimplified  Před 4 lety +1

      If you do from start, you can't be sure about first repeated value. Just take the example mentioned in video & try from starting.
      - Treeset won't help here. We use TreeSet for sorting & here we don't require sorting. Thanks

  • @pranitapasalkar5533
    @pranitapasalkar5533 Před 3 lety

    How to solve this using brute force

  • @anjalisingh-sx5ct
    @anjalisingh-sx5ct Před 3 lety

    Other than brute , hashing .....
    Any approach????

    • @piyushmathpal4244
      @piyushmathpal4244 Před 2 lety

      yes
      if(s.indexOf(i)!=s.lastIndexOf(i))
      return a.charAt(i);

  • @codeforthrone9569
    @codeforthrone9569 Před 3 lety

    any way to do this with 2 pointers method???