消除字符串数组的重复项的函数

public static String[] RemoveDup(String[] myData)
{
    if (myData.Length > 0)
    {
        Array.Sort(myData);
        int size = 1;
        for (int i = 1; i < myData.Length; i++)
            if (myData[i] != myData[i - 1])
                size++;
        String[] myTempData = new String[size];
        int j = 0;
        myTempData[j++] = myData[0];
        for (int i = 1; i < myData.Length; i++)
            if (myData[i] != myData[i - 1])
                myTempData[j++] = myData[i];
        return myTempData;
    }
    return myData;
}


using System;
using System.Linq;//有了Linq很多事变的简单了

namespace 排除重复行
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] a ={ "1", "1", "2", "1", "1", "3", "1", "1", };
            Console.WriteLine("原数组长度:    {0}", a.Length);
            Console.WriteLine("排除后数组长度:{0}",a.Distinct<string>().ToArray().Length);
            Console.ReadKey();
         }
     }

你可能感兴趣的:(消除数组重复项)