About generic List<string>

T

Tony Johansson

Hello!

I have a List collection with several identical strings. Is it any way to
count how many of each identical string I have in the list without
going through the list by using some iteration like foreach

I just wonder if it's possible to use linq in a suitable way here.

//Tony
 
S

Stanimir Stoyanov

Yes, LINQ would be of use here but there will be a loop through the distinct
strings. You can use the Count extension method to get the count of each of
them.

var distinctStrings = list.Distinct(); // 'list' is assumed to
be List<string>

foreach (string eachString in distinctStrings)
{
int results = data.Count(s => s == eachString);

// ...
}
 
G

Göran Andersson

Tony said:
Hello!

I have a List collection with several identical strings. Is it any way
to count how many of each identical string I have in the list without
going through the list by using some iteration like foreach

I just wonder if it's possible to use linq in a suitable way here.

//Tony

Yes, you can get groups with each string value and count the strings in
each group. Example:

List<string> names = new List<string> { "John", "Peter", "Paul", "Anna",
"Peter", "Anna", "Jane", "John", "Peter" };

var count = from string name in names group name by name into cnt select
new { Name = cnt.Key, Count = cnt.Count() };

foreach (var c in count) {
Console.WriteLine("{0} : {1}", c.Name, c.Count);
}
 
A

Alberto Poblacion

Tony Johansson said:
I have a List collection with several identical strings. Is it any way to
count how many of each identical string I have in the list without
going through the list by using some iteration like foreach

I just wonder if it's possible to use linq in a suitable way here.

The standard way to find duplicates in a table using SQL is to do a
"select Count(*) as repeats, MyField Group By MyField Having repeats>1".
There's no reason why you can't translate it into LINQ-to-objects if you
wish to apply it to a List<string>.

var duplicates = myList.GroupBy(x => x).Where(g => g.Count() >
1).Select(g => g.Key);
 
G

Göran Andersson

Alberto said:
The standard way to find duplicates in a table using SQL is to do a
"select Count(*) as repeats, MyField Group By MyField Having repeats>1".
There's no reason why you can't translate it into LINQ-to-objects if you
wish to apply it to a List<string>.

var duplicates = myList.GroupBy(x => x).Where(g => g.Count() >
1).Select(g => g.Key);

Hm... That won't return the count for each string.

It's not LINQ to Objects either...
 
B

Ben Voigt [C++ MVP]

Tony Johansson said:
Hello!

I have a List collection with several identical strings. Is it any way to
count how many of each identical string I have in the list without
going through the list by using some iteration like foreach

I just wonder if it's possible to use linq in a suitable way here.

Either you can iterate or you can use a function that iterates for you, such
as the IEnumerable ForAll extension method. Either way an iteration will
take place.


List<string> strs;
Dictionary<string, int> counts;

foreach (string s in strs) {
int tempCount;
counts.TryGetValue(s, out tempCount);
counts = 1 + tempCount;
}

foreach (KeyValuePair<string, int> result in counts) {
Debug.WriteLine(result.Key + " occurred " + result.Value + " time(s).");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top