✅ how to know the index of repeated items in list ?
I have an list with repeated values i want to know the index of this values !
P.S i will not delete this repeated values
14 Replies
A simple approach would be something like this
@Diddy yes and i will take that index to print them
When i use loop and compare with the value they stuck at first item
@🌈 Thinker 🌈 I'm not understanding the code !
It creates a generic static method
GetIndicies
which takes a generic list and a value, then it loops through each of the items in the list. If the item at the current index is equal to the item which was passed into the method, then it adds the index to a list. Finally it returns the list of indicies.
If you haven't encountered generic methods (<T>
) before, then I'd suggest reading up on generics.
Wow, I didn't know we could write it this way
It's the same thing as
List<int> indicies = new List<int>();
, just shorter.
Alternatively you can write var indicies = new List<int>();
Is it a good practice?
Makes your code shorter
Seems like it would save some efforts editing the names/types when you need a change
Thanks!
Can i use .IndexOf(,) ? With loop
@🌈 Thinker 🌈
It work ! Lol @🌈 Thinker 🌈
But give me out of range !
I will capture it to see how we can solve it
Since you're setting
index
to ddate.IndexOf(d, x)
, then the likely thing is that ddate
does not contains the value d
, and therefore index
is set to -1, which causes the exception.I put them inside try & catch
And worked fine ! Lol
Thank you @🌈 Thinker 🌈
That's just ignoring the issue but sure
Yes exactly that what i do lol
The deadline is today and I don't have time to fix it