C# 数组,如何使数组中的数据彼此不同?
C# 数组,如何使数组中的数据彼此不同? 例如
string[] a = {"a","b","a","c","b","b","c","a"};
如何获得
string[]b = {"a","b","c"}
C# Array, How to make data in an array distinct from each other?
For example
string[] a = {"a","b","a","c","b","b","c","a"};
how to get
string[]b = {"a","b","c"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是 LINQ
Distinct()
命令:Easiest way is the LINQ
Distinct()
command :您可能需要考虑使用 Set 而不是数组。集合不能包含重复项,因此添加第二个“a”不会产生任何效果。这样,您的字符集合将始终不包含重复项,并且您无需对其进行任何后期处理。
You might want to consider using a Set instead of an array. Sets can't contain duplicates so adding the second "a" would have no effect. That way your collection of characters will always contain no duplicates and you won't have to do any post processing on it.
开始时的数组是
IEnumerable
。IEnumerable
有一个Distinct()
方法,可用于将列表操作为不同的值最后,
IEnumerable
有一个ToArray()
方法:An array, which you start with, is
IEnumerable<T>
.IEnumerable<T>
has aDistinct()
method which can be used to manipulate the list into its distinct valuesFinally,
IEnumerable<T>
has aToArray()
method:我认为使用 c# Dictionary 是更好的方法,我可以使用 按值排序LINQ
I think using c# Dictionary is the better way and I can sort by value using LINQ