AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

Determine if string appears more than once in string array (C#)

I have an array of strings, f.e.

string [] letters = { "a", "a", "b", "c" };

I need to find a way to determine if any string in the array appears more than once. I thought the best way is to make a new string-array without the string in question and to use Contains,

foreach (string letter in letters)
{
    string [] otherLetters = //?
    if (otherLetters.Contains(letter))
    {
        //etc.     
    }
}

but I cannot figure out how. If anyone has a solution for this or a better approach, please answer.

Upvotes: 1

Views: 4316

Answers (4)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

The easiest way is to use GroupBy:

var lettersWithMultipleOccurences = letters.GroupBy(x => x)
                                           .Where(g => g.Count() > 1)
                                           .Select(g => g.Key);

This will first group your array using the letters as keys. It then returns only those groups with multiple entries and returns the key of these groups. As a result, you will have an IEnumerable<string> containing all letters that occur more than once in the original array. In your sample, this is only "a".

Beware: Because LINQ is implemented using deferred execution, enumerating lettersWithMultipleOccurences multiple times, will perform the grouping and filtering multiple times. To avoid this, call ToList() on the result:

var lettersWithMultipleOccurences = letters.GroupBy(x => x)
                                           .Where(g => g.Count() > 1)
                                           .Select(g => g.Key).
                                           .ToList();

lettersWithMultipleOccurences will now be of type List<string>.

Upvotes: 12

Peter
Peter

Reputation: 27934

A HashSet will give you good performance:

HashSet<string> hs = new HashSet<string>();
foreach (string letter in letters)
{
    if (hs.Contains(letter))
    {
        //etc. more as once     
    }
    else
    {
           hs.Add(letter);
    }
}

Upvotes: 1

Heinzi
Heinzi

Reputation: 172200

You can the LINQ extension methods:

if (letters.Distinct().Count() == letters.Count()) {
    // no duplicates
}

Enumerable.Distinct removes duplicates. Thus, letters.Distinct() would return three elements in your example.

Upvotes: 5

Konrad Rudolph
Konrad Rudolph

Reputation: 545498

Create a HashSet from the array and compare their sizes:

var set = new HashSet(letters);
bool hasDoubleLetters = set.Size == letters.Length;

Upvotes: 1

Related Questions