gpt4 book ai didi

c# - 在列表中查找连续的重复项

转载 作者:行者123 更新时间:2023-11-30 20:20:23 25 4
gpt4 key购买 nike

有很多方法可以找到列表中的重复项,是否有任何方法可以在列表中找到连续的重复项。

例如

List<string> stringList = new List<string>();
stringList.Add("Name1");
stringList.Add("Name2");
stringList.Add("Name1");

除了

应该找不到
stringList.Add("Name1");
stringList.Add("Name1");
stringList.Add("Name2");

应该返回 1 个条目

这会返回重复项。

 var q = listString.GroupBy(x => x)
.Select(g => new { Value = g.Key, Count = g.Count() })
.OrderByDescending(x => x.Count);

最佳答案

这是一种返回重复项及其索引的方法:

var duplicates =
stringList
.Select((x,i) => new {Item = x, Index = i})
.Skip(1) //We start with the second item
.Where(y => y.Item == stringList[y.Index-1])
.ToList();

关于c# - 在列表中查找连续的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36935592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com