C# 如何从 ARRAYLIST 中删除重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11272198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove duplicate value from ARRAYLIST
提问by Mitesh Machhi
i have one arraylist wich contain values from different database but it stores some duplicate values so, i want to remove duplicate values and store only unique value in ARRay List.
我有一个包含来自不同数据库的值的数组列表,但它存储了一些重复值,因此,我想删除重复值并仅在 ARRay 列表中存储唯一值。
So, how can this is achievable ?
那么,这是如何实现的呢?
Thanks in Advance... Mitesh
提前致谢... Mitesh
回答by brianestey
You can replace your ArrayList with a HashSet. From the documentation:
您可以用HashSet替换 ArrayList 。从文档:
The HashSet<T> class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
If it's absolutely necessary to use an ArrayList, you could use some Linq to remove duplicates with the Distinctcommand.
如果绝对有必要使用 ArrayList,您可以使用一些 Linq 通过Distinct命令删除重复项。
var distinctItems = arrayList.Distinct()
回答by The Original Android
If you must use ArrayList, use the Sortmethod. Here's a good link @ Sort Method of ArrayList. After the list is sorted, then use an algorithm to iterate/compare all your elements and remove the duplicates.
如果必须使用 ArrayList,请使用Sort方法。这是一个很好的链接@ ArrayList 的排序方法。列表排序后,然后使用算法迭代/比较所有元素并删除重复项。
Have fun,
玩得开心,
Tommy Kwee
汤米·奎
回答by Falanwe
If you can, you should use a HashSet, or any other set class. It is much more efficient for this kind of operation. The main default of the HashSet is that the ordering of the element is not guaranteed to remain the same as your original list (wich may or may not be a problem, depending on your specifications).
如果可以,您应该使用HashSet或任何其他集合类。这种操作的效率要高得多。HashSet 的主要默认值是不保证元素的顺序与原始列表相同(这可能是也可能不是问题,具体取决于您的规范)。
Otherwise, if you need to keep the ordering, but only need the duplicates removed when you enumerate through your values, you can use the Distinctmethod from linq. Just be careful and don't run this query and copy the result everytime you modify your arraylist as it risks impacting your performances.
否则,如果您需要保持顺序,但只需要在枚举值时删除重复项,则可以使用linq 中的Distinct方法。请小心,不要在每次修改数组列表时运行此查询并复制结果,因为它可能会影响您的性能。
回答by The Original Android
Let's try another method. Instead removing duplicates, avoid adding any duplicates. This might be more efficient in your environment. Here's a sample code:
让我们尝试另一种方法。而不是删除重复项,避免添加任何重复项。这在您的环境中可能更有效。这是一个示例代码:
ArrayList<String> myList = new ArrayList<string>();
foreach (string aString in myList)
{
if (!myList.Contains( aString ))
{
myList.Add(aString);
}
}
回答by sanjay
Hashtable ht = new Hashtable();
foreach (string item in originalArray){
//set a key in the hashtable for our arraylist value - leaving the hashtable value empty
ht[item] = null;
}
//now grab the keys from that hashtable into another arraylist
ArrayList distincArray = new ArrayList(ht.Keys);
回答by reza akhlaghi
you can using This code when work with an ArrayList
您可以在使用 ArrayList 时使用此代码
ArrayList arrayList;
//Add some Members :)
arrayList.Add("ali");
arrayList.Add("hadi");
arrayList.Add("ali");
//Remove duplicates from array
for (int i = 0; i < arrayList.Count; i++)
{
for (int j = i + 1; j < arrayList.Count ; j++)
if (arrayList[i].ToString() == arrayList[j].ToString())
arrayList.Remove(arrayList[j]);
}

