Not unique values ​​from a list of items in C #

I have a list

list = {1,1,1,2,3,3,3,4,4,5,6,6,6}

Now I want to have a list of non-unique values

The summary list contains only {2.5}

How can I do this via LINQ or any other function.

+3
source share
3 answers

One way is to use the GroupBy method and filter only those that have the number 1.

var nonUnique = list.GroupBy(l => l)
                    .Where(g => g.Count() == 1)
                    .Select(g => g.Key);
+10
source

Try the following:

List<int> list = new List<int>(new int[]{ 1, 1, 1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6});
List<int> unique=new List<int>();
int count=0;
bool dupFlag = false;
for (int i = 0; i < list.Count; i++)
{
count = 0;
dupFlag = false;
for(int j=0;j<list.Count;j++)
{
    if (i == j)
    continue;

    if (list[i].Equals(list[j]))
    {
    count++;
    if (count >= 1)
    {
        dupFlag = true;
        break;
    }
    }

}
if (!dupFlag)
    unique.Add(list[i]);
}
0
source

Try this code:

var lstUnique =
    from t1 in list
    group t1 by t1 into Gr
    where Gr.Count() == 1
    select Gr.Key;
0
source

All Articles