How do I remove duplicates from a C# array?

后端 未结 27 2201
北海茫月
北海茫月 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:46
    public static int RemoveDuplicates(ref int[] array)
    {
        int size = array.Length;
    
        // if 0 or 1, return 0 or 1:
        if (size  < 2) {
            return size;
        }
    
        int current = 0;
        for (int candidate = 1; candidate < size; ++candidate) {
            if (array[current] != array[candidate]) {
                array[++current] = array[candidate];
            }
        }
    
        // index to count conversion:
        return ++current;
    }
    
    0 讨论(0)
  • 2020-11-22 08:46

    Below is an simple logic in java you traverse elements of array twice and if you see any same element you assign zero to it plus you don't touch the index of element you are comparing.

    import java.util.*;
    class removeDuplicate{
    int [] y ;
    
    public removeDuplicate(int[] array){
        y=array;
    
        for(int b=0;b<y.length;b++){
            int temp = y[b];
            for(int v=0;v<y.length;v++){
                if( b!=v && temp==y[v]){
                    y[v]=0;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:47

    NOTE : NOT tested!

    string[] test(string[] myStringArray)
    {
        List<String> myStringList = new List<string>();
        foreach (string s in myStringArray)
        {
            if (!myStringList.Contains(s))
            {
                myStringList.Add(s);
            }
        }
        return myStringList.ToString();
    }
    

    Might do what you need...

    EDIT Argh!!! beaten to it by rob by under a minute!

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

    This might depend on how much you want to engineer the solution - if the array is never going to be that big and you don't care about sorting the list you might want to try something similar to the following:

        public string[] RemoveDuplicates(string[] myList) {
            System.Collections.ArrayList newList = new System.Collections.ArrayList();
    
            foreach (string str in myList)
                if (!newList.Contains(str))
                    newList.Add(str);
            return (string[])newList.ToArray(typeof(string));
        }
    
    0 讨论(0)
  • 2020-11-22 08:48

    Add all the strings to a dictionary and get the Keys property afterwards. This will produce each unique string, but not necessarily in the same order your original input had them in.

    If you require the end result to have the same order as the original input, when you consider the first occurance of each string, use the following algorithm instead:

    1. Have a list (final output) and a dictionary (to check for duplicates)
    2. For each string in the input, check if it exists in the dictionary already
    3. If not, add it both to the dictionary and to the list

    At the end, the list contains the first occurance of each unique string.

    Make sure you consider things like culture and such when constructing your dictionary, to make sure you handle duplicates with accented letters correctly.

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

    -- This is Interview Question asked every time. Now i done its coding.

    static void Main(string[] args)
    {    
                int[] array = new int[] { 4, 8, 4, 1, 1, 4, 8 };            
                int numDups = 0, prevIndex = 0;
    
                for (int i = 0; i < array.Length; i++)
                {
                    bool foundDup = false;
                    for (int j = 0; j < i; j++)
                    {
                        if (array[i] == array[j])
                        {
                            foundDup = true;
                            numDups++; // Increment means Count for Duplicate found in array.
                            break;
                        }                    
                    }
    
                    if (foundDup == false)
                    {
                        array[prevIndex] = array[i];
                        prevIndex++;
                    }
                }
    
                // Just Duplicate records replce by zero.
                for (int k = 1; k <= numDups; k++)
                {               
                    array[array.Length - k] = '\0';             
                }
    
    
                Console.WriteLine("Console program for Remove duplicates from array.");
                Console.Read();
            }
    
    0 讨论(0)
提交回复
热议问题