Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if there are no repeats in the list

Tags:

c#

list

contains

I have a list called com, which contains a bunch of integers. I need to check the list to make sure that each integer only exists once in the list. So if:

com{1,2,3,4,1,3}

I need have some code to check that 1 is represented twice as well as 3. This is my best guess on how to solve it:

for (int j = 0; j < com.Count; j++)
        {
            if (com.Contains(com[j]))
            {
                lion += 1;
            }
            else
            {
                lion = 0;
            }   
        }

But it doesn't work. Can anybody out there help me??

like image 711
Oedum Avatar asked Jan 27 '26 01:01

Oedum


1 Answers

Here's a simple, but probably not that efficient way using LINQ:

using System.Linq;

...

bool containsRepeats = com.Count() != com.Distinct().Count();
like image 130
George Duckett Avatar answered Jan 28 '26 14:01

George Duckett



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!