Array - 19: Find the first repeating value in array
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
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
Good work. Keep it up !
Thanks
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
can we do it with linked hashmap
I do not understand what will happen if we begin from starting. and if we take Tree set? explain plz
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
How to solve this using brute force
Other than brute , hashing .....
Any approach????
yes
if(s.indexOf(i)!=s.lastIndexOf(i))
return a.charAt(i);
any way to do this with 2 pointers method???
It won't work
@@anjalisingh-sx5ct why?? Can u plz explain