Skip to content

Instantly share code, notes, and snippets.

@xiaomi7732
Created September 22, 2022 19:11
Show Gist options
  • Save xiaomi7732/9b4632d57b0159c3e887c36ef3fa23df to your computer and use it in GitHub Desktop.
Save xiaomi7732/9b4632d57b0159c3e887c36ef3fa23df to your computer and use it in GitHub Desktop.
Detect duplications in a list in real projects

Detect duplications in real project scenario

Disclaimer: This is not leetcode implemenation, complexity is not considered. This is looking for an easy implemenation.

Problem

Assuming a list of string:

List<string> data = ...

Find out:

  1. Is there duplications;
  2. What are the duplications;
  3. How many times did it duplicated;

This works

List<string> data = new List<string> { "a","e", "c", "b", "c", "c", "d","e" };

IEnumerable<(string Dupe, int Count)> moreThanOnce = data
    .GroupBy(d => d)                    // Group by the value of the strings, using the string value as keys
    .Where(g => g.Count() > 1)          // For a given key, how many items does it have? If it have more than 1, it's a dup.
    .Select(g => (g.Key, g.Count()));   // Output the key, and the count as a Triple<string, int>.
    
// Output. Or could be anything else.
foreach (var item in moreThanOnce)
{
    Console.WriteLine($"{item.Dupe}: {item.Count}");
}

This is a trap

List<string> data = new List<string> { "a", "e", "c", "b", "c", "c", "d", "e" };
List<string> distinct = data.Distinct().ToList();

IEnumerable<string> dup = data.Except(distinct);  // Not working, it will exclude everything from data.

Console.WriteLine(dup.Any()); // It is always false!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment