How do I remove duplicates from a C# array?

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

    This code 100% remove duplicate values from an array[as I used a[i]].....You can convert it in any OO language..... :)

    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++)
                {
                     a[k]=a[k+1];
                }
                j--;
                size--;
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 08:25

    Find answer below.

    class Program
    {
        static void Main(string[] args)
        {
            var nums = new int[] { 1, 4, 3, 3, 3, 5, 5, 7, 7, 7, 7, 9, 9, 9 };
            var result = removeDuplicates(nums);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
        }
        static int[] removeDuplicates(int[] nums)
        {
            nums = nums.ToList().OrderBy(c => c).ToArray();
            int j = 1;
            int i = 0;
            int stop = 0;
            while (j < nums.Length)
            {
                if (nums[i] != nums[j])
                {
                    nums[i + 1] = nums[j];
                    stop = i + 2;
                    i++;
                }
                j++;
            }
            nums = nums.Take(stop).ToArray();
            return nums;
        }
    }
    

    Just a bit of contribution based on a test i just solved, maybe helpful and open to improvement by other top contributors here. Here are the things i did:

    1. I used OrderBy which allows me order or sort the items from smallest to the highest using LINQ
    2. I then convert it to back to an array and then re-assign it back to the primary datasource
    3. So i then initialize j which is my right hand side of the array to be 1 and i which is my left hand side of the array to be 0, i also initialize where i would i to stop to be 0.
    4. I used a while loop to increment through the array by going from one position to the other left to right, for each increment the stop position is the current value of i + 2 which i will use later to truncate the duplicates from the array.
    5. I then increment by moving from left to right from the if statement and from right to right outside of the if statement until i iterate through the entire values of the array.
    6. I then pick from the first element to the stop position which becomes the last i index plus 2. that way i am able to remove all the duplicate items from the int array. which is then reassigned.
    0 讨论(0)
  • 2020-11-22 08:26

    The following piece of code attempts to remove duplicates from an ArrayList though this is not an optimal solution. I was asked this question during an interview to remove duplicates through recursion, and without using a second/temp arraylist:

    private void RemoveDuplicate() 
    {
    
    ArrayList dataArray = new ArrayList(5);
    
                dataArray.Add("1");
                dataArray.Add("1");
                dataArray.Add("6");
                dataArray.Add("6");
                dataArray.Add("6");
                dataArray.Add("3");
                dataArray.Add("6");
                dataArray.Add("4");
                dataArray.Add("5");
                dataArray.Add("4");
                dataArray.Add("1");
    
                dataArray.Sort();
    
                GetDistinctArrayList(dataArray, 0);
    }
    
    private void GetDistinctArrayList(ArrayList arr, int idx)
    
    {
    
                int count = 0;
    
                if (idx >= arr.Count) return;
    
                string val = arr[idx].ToString();
                foreach (String s in arr)
                {
                    if (s.Equals(arr[idx]))
                    {
                        count++;
                    }
                }
    
                if (count > 1)
                {
                    arr.Remove(val);
                    GetDistinctArrayList(arr, idx);
                }
                else
                {
                    idx += 1;
                    GetDistinctArrayList(arr, idx);
                }
            }
    
    0 讨论(0)
  • 2020-11-22 08:26
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    
    namespace Rextester
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                 List<int> listofint1 = new List<int> { 4, 8, 4, 1, 1, 4, 8 };
               List<int> updatedlist= removeduplicate(listofint1);
                foreach(int num in updatedlist)
                   Console.WriteLine(num);
            }
    
    
            public static List<int> removeduplicate(List<int> listofint)
             {
                 List<int> listofintwithoutduplicate= new List<int>();
    
    
                  foreach(var num in listofint)
                     {
                      if(!listofintwithoutduplicate.Any(p=>p==num))
                            {
                              listofintwithoutduplicate.Add(num);
                            }
                      }
                 return listofintwithoutduplicate;
             }
        }
    
    
    
    }
    
    0 讨论(0)
  • 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--;
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-22 08:28
    List<String> myStringList = new List<string>();
    foreach (string s in myStringArray)
    {
        if (!myStringList.Contains(s))
        {
            myStringList.Add(s);
        }
    }
    

    This is O(n^2), which won't matter for a short list which is going to be stuffed into a combo, but could be rapidly be a problem on a big collection.

    0 讨论(0)
提交回复
热议问题