如何从 C# 数组中删除重复项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9673/
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 do I remove duplicates from a C# array?
提问by lomaxx
I have been working with a string[]
array in C# that gets returned from a function call. I could possibly cast to a Generic
collection, but I was wondering if there was a better way to do it, possibly by using a temp array.
我一直在使用string[]
C# 中的数组,该数组从函数调用中返回。我可能会转换为一个Generic
集合,但我想知道是否有更好的方法来做到这一点,可能是使用临时数组。
What is the best way to remove duplicates from a C# array?
从 C# 数组中删除重复项的最佳方法是什么?
采纳答案by Jeff Atwood
You could possibly use a LINQ query to do this:
您可以使用 LINQ 查询来执行此操作:
int[] s = { 1, 2, 3, 3, 4};
int[] q = s.Distinct().ToArray();
回答by Matthew Schinckel
If you needed to sort it, then you could implement a sort that also removes duplicates.
如果您需要对其进行排序,那么您可以实现一种同时删除重复项的排序。
Kills two birds with one stone, then.
那就用一颗石头杀死两只鸟。
回答by Lasse V. Karlsen
Add all the strings to a dictionary and get the Keys property afterwards. This will produce each unique string, but not necessarily in the same order your original input had them in.
将所有字符串添加到字典中,然后获取 Keys 属性。这将产生每个唯一的字符串,但不一定与原始输入的顺序相同。
If you require the end result to have the same order as the original input, when you consider the first occurance of each string, use the following algorithm instead:
如果您要求最终结果与原始输入的顺序相同,则在考虑每个字符串的第一次出现时,请改用以下算法:
- Have a list (final output) and a dictionary (to check for duplicates)
- For each string in the input, check if it exists in the dictionary already
- If not, add it both to the dictionary and to the list
- 有一个列表(最终输出)和一个字典(检查重复)
- 对于输入中的每个字符串,检查它是否已经存在于字典中
- 如果没有,请将其添加到字典和列表中
At the end, the list contains the first occurance of each unique string.
最后,列表包含每个唯一字符串的第一次出现。
Make sure you consider things like culture and such when constructing your dictionary, to make sure you handle duplicates with accented letters correctly.
确保在构建字典时考虑文化等因素,以确保正确处理带有重音字母的重复项。
回答by GateKiller
The following tested and working code will remove duplicates from an array. You must include the System.Collections namespace.
以下经过测试和工作的代码将从数组中删除重复项。您必须包括 System.Collections 命名空间。
string[] sArray = {"a", "b", "b", "c", "c", "d", "e", "f", "f"};
var sList = new ArrayList();
for (int i = 0; i < sArray.Length; i++) {
if (sList.Contains(sArray[i]) == false) {
sList.Add(sArray[i]);
}
}
var sNew = sList.ToArray();
for (int i = 0; i < sNew.Length; i++) {
Console.Write(sNew[i]);
}
You could wrap this up into a function if you wanted to.
如果你愿意,你可以把它包装成一个函数。
回答by rjzii
This might depend on how much you want to engineer the solution - if the array is never going to be that big and you don't care about sorting the list you might want to try something similar to the following:
这可能取决于您想要设计解决方案的程度 - 如果数组永远不会那么大并且您不关心对列表进行排序,您可能想要尝试类似于以下内容:
public string[] RemoveDuplicates(string[] myList) {
System.Collections.ArrayList newList = new System.Collections.ArrayList();
foreach (string str in myList)
if (!newList.Contains(str))
newList.Add(str);
return (string[])newList.ToArray(typeof(string));
}
回答by ZombieSheep
NOTE : NOT tested!
注意:未测试!
string[] test(string[] myStringArray)
{
List<String> myStringList = new List<string>();
foreach (string s in myStringArray)
{
if (!myStringList.Contains(s))
{
myStringList.Add(s);
}
}
return myStringList.ToString();
}
Might do what you need...
可能会做你需要的...
EDITArgh!!! beaten to it by rob by under a minute!
编辑啊!!!不到一分钟就被抢了!
回答by Arcturus
Here is the HashSet<string>approach:
这是HashSet<string>方法:
public static string[] RemoveDuplicates(string[] s)
{
HashSet<string> set = new HashSet<string>(s);
string[] result = new string[set.Count];
set.CopyTo(result);
return result;
}
Unfortunately this solution also requires .NET framework 3.5 or later as HashSet was not added until that version. You could also use array.Distinct(), which is a feature of LINQ.
不幸的是,此解决方案还需要 .NET framework 3.5 或更高版本,因为直到该版本才添加 HashSet。您还可以使用array.Distinct(),这是 LINQ 的一个特性。
回答by Will Dean
List<String> myStringList = new List<string>(); foreach (string s in myStringArray) { if (!myStringList.Contains(s)) { myStringList.Add(s); } }
List<String> myStringList = new List<string>(); foreach (string s in myStringArray) { if (!myStringList.Contains(s)) { myStringList.Add(s); } }
This is O(n^2), which won't matter for a short list which is going to be stuffed into a combo, but could be rapidly be a problem on a big collection.
这是O(n^2),这对于将被填充到组合中的短列表无关紧要,但可能很快就会成为大集合的问题。
回答by Sesh
Here is a O(n*n)approach that uses O(1)space.
这是使用O(1)空间的O(n*n)方法。
void removeDuplicates(char* strIn)
{
int numDups = 0, prevIndex = 0;
if(NULL != strIn && *strIn != 'private void RemoveDuplicate()
{
ArrayList dataArray = new ArrayList(5);
dataArray.Add("1");
dataArray.Add("1");
dataArray.Add("6");
dataArray.Add("6");
dataArray.Add("6");
dataArray.Add("3");
dataArray.Add("6");
dataArray.Add("4");
dataArray.Add("5");
dataArray.Add("4");
dataArray.Add("1");
dataArray.Sort();
GetDistinctArrayList(dataArray, 0);
}
private void GetDistinctArrayList(ArrayList arr, int idx)
{
int count = 0;
if (idx >= arr.Count) return;
string val = arr[idx].ToString();
foreach (String s in arr)
{
if (s.Equals(arr[idx]))
{
count++;
}
}
if (count > 1)
{
arr.Remove(val);
GetDistinctArrayList(arr, idx);
}
else
{
idx += 1;
GetDistinctArrayList(arr, idx);
}
}
')
{
int len = strlen(strIn);
for(int i = 0; i < len; i++)
{
bool foundDup = false;
for(int j = 0; j < i; j++)
{
if(strIn[j] == strIn[i])
{
foundDup = true;
numDups++;
break;
}
}
if(foundDup == false)
{
strIn[prevIndex] = strIn[i];
prevIndex++;
}
}
strIn[len-numDups] = '##代码##';
}
}
The hash/linqapproaches above are what you would generally use in real life. However in interviews they usually want to put some constraints e.g. constant space which rules out hash or no internal api- which rules out using LINQ.
上面的hash/linq方法是您在现实生活中通常会使用的方法。然而,在面试中,他们通常希望设置一些约束,例如排除散列或没有内部api 的常量空间- 排除使用LINQ。
回答by Vijay Swami
The following piece of code attempts to remove duplicates from an ArrayList though this is not an optimal solution. I was asked this question during an interview to remove duplicates through recursion, and without using a second/temp arraylist:
以下代码尝试从 ArrayList 中删除重复项,尽管这不是最佳解决方案。我在面试中被问到这个问题是通过递归删除重复项,并且不使用第二个/临时数组列表:
##代码##