How do I remove duplicates from a C# array?

后端 未结 27 2685
北海茫月
北海茫月 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:38

      private static string[] distinct(string[] inputArray)
            {
                bool alreadyExists;
                string[] outputArray = new string[] {};
    
                for (int i = 0; i < inputArray.Length; i++)
                {
                    alreadyExists = false;
                    for (int j = 0; j < outputArray.Length; j++)
                    {
                        if (inputArray[i] == outputArray[j])
                            alreadyExists = true;
                    }
                            if (alreadyExists==false)
                            {
                                Array.Resize(ref outputArray, outputArray.Length + 1);
                                outputArray[outputArray.Length-1] = inputArray[i];
                            }
                }
                return outputArray;
            }
    

提交回复
热议问题