Giu*_*ano 202
在.NET framework 3.5及更高版本中,您可以使用Enumerable.GroupBy它返回可枚举的重复键的可枚举,然后过滤掉任何Count <= 1的枚举,然后选择它们的键以返回到单个可枚举:
var duplicateKeys = list.GroupBy(x => x)
.Where(group => group.Count() > 1)
.Select(group => group.Key);
Run Code Online (Sandbox Code Playgroud)
ICR*_*ICR 31
如果您使用的是LINQ,则可以使用以下查询:
var duplicateItems = from x in list
group x by x into grouped
where grouped.Count() > 1
select grouped.Key;
Run Code Online (Sandbox Code Playgroud)
或者,如果你喜欢没有语法糖:
var duplicateItems = list.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key);
Run Code Online (Sandbox Code Playgroud)
这会将所有相同的元素分组,然后仅过滤到具有多个元素的组.最后,它只选择那些组中的密钥,因为您不需要计数.
如果您不想使用LINQ,可以使用此扩展方法:
public void SomeMethod {
var duplicateItems = list.GetDuplicates();
…
}
public static IEnumerable<T> GetDuplicates<T>(this IEnumerable<T> source) {
HashSet<T> itemsSeen = new HashSet<T>();
HashSet<T> itemsYielded = new HashSet<T>();
foreach (T item in source) {
if (!itemsSeen.Add(item)) {
if (itemsYielded.Add(item)) {
yield return item;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可以跟踪它已经看到和产生的物品.如果之前没有看过某个项目,它会将其添加到所看到的项目列表中,否则会忽略它.如果它之前没有产生一个项目,它会产生它,否则它会忽略它.
evi*_*one 19
没有LINQ:
string[] ss = {"1","1","1"};
var myList = new List<string>();
var duplicates = new List<string>();
foreach (var s in ss)
{
if (!myList.Contains(s))
myList.Add(s);
else
duplicates.Add(s);
}
// show list without duplicates
foreach (var s in myList)
Console.WriteLine(s);
// show duplicates list
foreach (var s in duplicates)
Console.WriteLine(s);
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找更通用的方法:
public static List<U> FindDuplicates<T, U>(this List<T> list, Func<T, U> keySelector)
{
return list.GroupBy(keySelector)
.Where(group => group.Count() > 1)
.Select(group => group.Key).ToList();
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是一个例子:
public class Person {
public string Name {get;set;}
public int Age {get;set;}
}
List<Person> list = new List<Person>() { new Person() { Name = "John", Age = 22 }, new Person() { Name = "John", Age = 30 }, new Person() { Name = "Jack", Age = 30 } };
var duplicateNames = list.FindDuplicates(p => p.Name);
var duplicateAges = list.FindDuplicates(p => p.Age);
foreach(var dupName in duplicateNames) {
Console.WriteLine(dupName); // Will print out John
}
foreach(var dupAge in duplicateAges) {
Console.WriteLine(dupAge); // Will print out 30
}
Run Code Online (Sandbox Code Playgroud)
使用LINQ,当然.下面的代码将为您提供item的字典作为字符串,以及您的源代码列表中每个项目的计数.
var item2ItemCount = list.GroupBy(item => item).ToDictionary(x=>x.Key,x=>x.Count());
Run Code Online (Sandbox Code Playgroud)