Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid duplicate value in list Using C# in unity

I am newbie in unity and use C#, actually i am python developer i try to make a list which can holds only unique Values and if some duplicate value come it will not allow to enter in list

List<int> iList = new List<int>();
    iList.Add(2);
    iList.Add(3);
    iList.Add(5);
    iList.Add(7);

list =[2,3,5,7]

**in python we just do this to avoid duplicate in list **

if(iList.indexof(value)!=-1){
iList.append(value)
}

But what should we do in C# to achieve very similar results Thanks Your effort will be highly appreciated

like image 871
Ahmad Avatar asked Mar 03 '26 19:03

Ahmad


1 Answers

C# List has similar method: if (!iList.Contains(value)) iList.Add(value);

Alternatively you can use a HashSet<int>. There you don't need to add any conditions:

var hasSet = new HashSet<int>(); 
hashSet.Add(1);
hashSet.Add(1);
like image 112
Maxim Goncharuk Avatar answered Mar 06 '26 10:03

Maxim Goncharuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!