本文介绍了排除列表中的元素连续的重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有好的方式来消除的连续的重复列表中的元素的
Is there a "nice" way to eliminate consecutive duplicates of list elements?
例如:
Example:
["red"; "red"; "blue"; "green"; "green"; "red"; "red"; "yellow"; "white"; "white"; "red"; "white"; "white"]
应成为
should become
["red"; "blue"; "green"; "red"; "yellow"; "white"; "red"; "white"]
- 按好我的意思是最易于阅读和理解到一个新的用户,并快速执行:)
--By "nice" I mean most readable and understandable to a new user and fast execution :)
推荐答案
一个简单而非常可读的解决方案:
A simple and very readable solution:
List<string> results = new List<string>();
foreach (var element in array)
{
if(results.Count == 0 || results.Last() != element)
results.Add(element);
}
这篇关于排除列表中的元素连续的重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!