2014-02-17 94 views
3

我有一个列表非独特价值

列表= {1,1,1,2,3,3,3,4,4,5,6, 6,6}

现在我想有非唯一值的列表

最后名单包含{2,5}仅

我如何能做到这一点通过LINQ或任何其他功能。

+2

只是很挑剔,但我想你会发现'{2,5}'是_unique_值的列表,而不是非唯一的列表。 – paxdiablo

+0

我有点困惑,但2和5是唯一的值。他们不是吗? o0只有2个中的一个和5个中的一个......你想达到什么目的? – Dmytro

回答

10

的一种方法是使用的GroupBy方法和过滤只有那些有1

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

计数试试这个:

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

试试这个代码:

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