-3

Let's say I have this string:

string common = "Blue;Red;Red;Green;Green;Green;Blue;Blue;Blue;Yellow";

Now I want to count, how often these words appear in this string.

I don't know how the string is going to look like. It's diffrent every time.

I want a variable for each word in the string, that contains a number how often the word appeared in the string.

So I should get something like:

Blue: 4
Red: 2
Green: 3
Yellow: 1
Liam
  • 27,717
  • 28
  • 128
  • 190
Kevin Mueller
  • 628
  • 3
  • 10
  • 23
  • 1
    Google hashtable/dictionary – Steve May 11 '18 at 15:40
  • 3
    @Steve I think a much easier way would be to just using Linq. Split on `;`, `GroupBy(x => x)`, and then display key and `x.Count()` – maccettura May 11 '18 at 15:41
  • 2
    Regex.Matches(input, "true").Count – Phil N DeBlanc May 11 '18 at 15:43
  • 1
    OP, you need to post your attempt and what _specifically_ is not working. Stack Overflow is not a code writing service – maccettura May 11 '18 at 15:43
  • 1
    [Solving problems can be hard work. When we have exhausted our own knowledge, it’s often tempting to simply ask someone else to solve the problem for us. It is very common to find the question we have is one many people have already experienced, and many of these people have already asked about it and have received correct answers in response. Because of the vast amount of information on the internet and Stack Overflow, it often takes just a simple search or two to find them.](http://idownvotedbecau.se/noresearch/) – Liam May 11 '18 at 15:43
  • @maccettura Dictionary is 1000% easier to understand for beginners than linq + lambda. Trust me.. – Steve May 11 '18 at 15:43
  • @Steve I could not disagree more in this scenario. Your solution would be incredibly inefficient and less elegant then a single line Linq query. Linq is a powerful tool in C# and it should be used where it makes sense, this scenario is easy to solve with Linq and can be understood by even the newest user of the language – maccettura May 11 '18 at 15:44
  • @Steve although I do agree that storing the information in a Dictionary would be ideal, using `ToDictionary()` would be the way to do it (but that _is Linq_) – maccettura May 11 '18 at 15:53
  • @maccettura I agree that a Linq solution shouldn't be that hard to understand, but a regular `for` loop and populating a dictionary wouldn't be incredibly inefficient either. – juharr May 11 '18 at 15:54
  • @juharr I used poor wording on my comment but it is too late to edit. I did not mean inefficient in the context of the execution, just inefficient to write all that extra code when simple Linq does this for us. Apologizes to Steve if that was not clear – maccettura May 11 '18 at 15:56

1 Answers1

3
string common = "Blue;Red;Red;Green;Green;Green;Blue;Blue;Blue;Yellow";
var grouped = common.Split(';')
    .GroupBy(x => x)
    .Select(x => new {
        x.Key,
        Count = x.Count(),
    });

foreach(var grouping in grouped)
{
    Console.WriteLine($"{grouping.Key}: {grouping.Count}");
}

Outputs:

Blue: 4
Red: 2
Green: 3
Yellow: 1

Alternatively (per comments about ToDictionary), this could work:

var grouped = common.Split(';')
    .GroupBy(x => x)
    .ToDictionary(x => x.Key, x => x.Count());

And in the display loop, change {grouping.Count} to {grouping.Value}

David Fox
  • 10,603
  • 9
  • 50
  • 80
  • 2
    No need to even have the `grouped` variable. Or to project to an anonymous object – maccettura May 11 '18 at 15:50
  • 4
    Or `ToDictionary(x => x.Key, x => x.Count())` instead of that `Select` to get a nice dictionary to do look-ups on. – juharr May 11 '18 at 15:52