How do I remove duplicates from a C# array?

后端 未结 27 2210
北海茫月
北海茫月 2020-11-22 07:53

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 w

27条回答
  •  既然无缘
    2020-11-22 08:26

    int size = a.Length;
            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++)
                        {
                            if (k != size - 1)
                            {
                                int temp = a[k];
                                a[k] = a[k + 1];
                                a[k + 1] = temp;
    
                            }
                        }
                        j--;
                        size--;
                    }
                }
            }
    

提交回复
热议问题