Combining 2 lists and and remove duplicates .Output in a third list .My attempts do not work

后端 未结 4 1078
孤街浪徒
孤街浪徒 2020-12-20 21:32

I always seem to have a problem when I need to compare 2 list and produce a 3rd list which include all unique items.I need to perform this quite often.

Attempt to re

4条回答
  •  旧时难觅i
    2020-12-20 22:21

    I had a similar problem where I had two very large lists with random strings.

    I made a recursive function which returns a new list with unique strings. I compared two lists with 100k random strings(it may or may not exist duplicates) each with 6 characters of abcdefghijklmnopqrstuvwxyz1234567890 and it was done in about 230 ms. I only measured the given function.

    I hope this will give value to someone.

    Image of test run

    makeCodesUnique(List existing, List newL)
    {
        // Get all duplicate between two lists
        List duplicatesBetween = newL.Intersect(existing).ToList();
    
        // Get all duplicates within list
        List duplicatesWithin = newL.GroupBy(x => x)
        .Where(group => group.Count() > 1)
        .Select(group => group.Key).ToList();
    
        if (duplicatesBetween.Count == 0 && duplicatesWithin.Count == 0)
        {
            // Return list if there are no duplicates
            return newL; 
        }
        else
        {
            if (duplicatesBetween.Count != 0)
            {
                foreach (string duplicateCode in duplicatesBetween)
                {
                    newL.Remove(duplicateCode);
                }
    
                // Generate new codes to substitute the removed ones
                List newCodes = generateSomeMore(duplicatesBetween.Count);
                newL.AddRange(newCodes);
                makeCodesUnique(existing, newL);
            }
            else if (duplicatesWithin.Count != 0)
            {
                foreach (string duplicateCode in duplicatesWithin)
                {
                    newL.Remove(duplicateCode);
                }
                List newCodes = generateSomeMore(duplicatesWithin.Count);
                new.AddRange(newCodes);
                makeCodesUnique(existing, newL);
            }
        }
        return newL;
    }
    

提交回复
热议问题