How do I remove duplicates from a C# array?

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

    you can using This code when work with an ArrayList

    ArrayList arrayList;
    //Add some Members :)
    arrayList.Add("ali");
    arrayList.Add("hadi");
    arrayList.Add("ali");
    
    //Remove duplicates from array
      for (int i = 0; i < arrayList.Count; i++)
        {
           for (int j = i + 1; j < arrayList.Count ; j++)
               if (arrayList[i].ToString() == arrayList[j].ToString())
                     arrayList.Remove(arrayList[j]);
    

提交回复
热议问题