How do I remove duplicates from a C# array?

后端 未结 27 2149
北海茫月
北海茫月 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<string>(ref outputArray, outputArray.Length + 1);
                                outputArray[outputArray.Length-1] = inputArray[i];
                            }
                }
                return outputArray;
            }
    
    0 讨论(0)
  • 2020-11-22 08:39

    The following tested and working code will remove duplicates from an array. You must include the System.Collections namespace.

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

    You could wrap this up into a function if you wanted to.

    0 讨论(0)
  • 2020-11-22 08:41

    Simple solution:

    using System.Linq;
    ...
    
    public static int[] Distinct(int[] handles)
    {
        return handles.ToList().Distinct().ToArray();
    }
    
    0 讨论(0)
  • 2020-11-22 08:41
    strINvalues = "1,1,2,2,3,3,4,4";
    strINvalues = string.Join(",", strINvalues .Split(',').Distinct().ToArray());
    Debug.Writeline(strINvalues);
    

    Kkk Not sure if this is witchcraft or just beautiful code

    1 strINvalues .Split(',').Distinct().ToArray()

    2 string.Join(",", XXX);

    1 Splitting the array and using Distinct [LINQ] to remove duplicates 2 Joining it back without the duplicates.

    Sorry I never read the text on StackOverFlow just the code. it make more sense than the text ;)

    0 讨论(0)
  • 2020-11-22 08:43

    Removing duplicate and ignore case sensitive using Distinct & StringComparer.InvariantCultureIgnoreCase

    string[] array = new string[] { "A", "a", "b", "B", "a", "C", "c", "C", "A", "1" };
    var r = array.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
    Console.WriteLine(r.Count); // return 4 items
    
    0 讨论(0)
  • 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]);
    
    0 讨论(0)
提交回复
热议问题