我一直在使用从函数调用中返回的c#字符串[]数组。我可以强制转换为Generic集合,但我想知道是否有更好的方法,可能是使用临时数组。

从c#数组中删除重复项的最佳方法是什么?


当前回答

你可以使用一个LINQ查询来做到这一点:

int[] s = { 1, 2, 3, 3, 4};
int[] q = s.Distinct().ToArray();

其他回答

所以我在做一个面试时,得到了同样的问题来分类和区分

static void Sort()
    {
        try
        {
            int[] number = new int[Convert.ToInt32(Console.ReadLine())];
            for (int i = 0; i < number.Length; i++)
            {
                number[i] = Convert.ToInt32(Console.ReadLine());
            }
            Array.Sort(number);
            int[] num = number.Distinct().ToArray();
            for (int i = 0; i < num.Length; i++)
            {
                Console.WriteLine(num[i]);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        Console.Read();
    }

这段代码从数组中100%删除重复值[因为我使用了一个[I]].....您可以将其转换为任何OO语言.....:)

for(int i=0;i<size;i++)
{
    for(int j=i+1;j<size;j++)
    {
        if(a[i] == a[j])
        {
            for(int k=j;k<size;k++)
            {
                 a[k]=a[k+1];
            }
            j--;
            size--;
        }
    }

}

简单的解决方案:

using System.Linq;
...

public static int[] Distinct(int[] handles)
{
    return handles.ToList().Distinct().ToArray();
}

下面经过测试和工作的代码将从数组中删除重复项。你必须包括系统。集合名称空间。

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]);
}

如果你愿意,你可以把它打包成一个函数。

在下面找到答案。

class Program
{
    static void Main(string[] args)
    {
        var nums = new int[] { 1, 4, 3, 3, 3, 5, 5, 7, 7, 7, 7, 9, 9, 9 };
        var result = removeDuplicates(nums);
        foreach (var item in result)
        {
            Console.WriteLine(item);
        }
    }
    static int[] removeDuplicates(int[] nums)
    {
        nums = nums.ToList().OrderBy(c => c).ToArray();
        int j = 1;
        int i = 0;
        int stop = 0;
        while (j < nums.Length)
        {
            if (nums[i] != nums[j])
            {
                nums[i + 1] = nums[j];
                stop = i + 2;
                i++;
            }
            j++;
        }
        nums = nums.Take(stop).ToArray();
        return nums;
    }
}

这是基于我刚刚解决的一个测试的一点贡献,可能对这里其他顶级贡献者的改进有所帮助。 以下是我所做的事情:

I used OrderBy which allows me order or sort the items from smallest to the highest using LINQ I then convert it to back to an array and then re-assign it back to the primary datasource So i then initialize j which is my right hand side of the array to be 1 and i which is my left hand side of the array to be 0, i also initialize where i would i to stop to be 0. I used a while loop to increment through the array by going from one position to the other left to right, for each increment the stop position is the current value of i + 2 which i will use later to truncate the duplicates from the array. I then increment by moving from left to right from the if statement and from right to right outside of the if statement until i iterate through the entire values of the array. I then pick from the first element to the stop position which becomes the last i index plus 2. that way i am able to remove all the duplicate items from the int array. which is then reassigned.