-3

trying to get a count of each item in the array, however its adding a total number.

  Dictionary<string, int> counts = new Dictionary<string, int>() {
          { "HelloWorld", 0},
          { "Hello", 0},
          { "World", 0},
          { "integer", 0},
        };


        foreach (var item in arraylist)
        {
            counts["HelloWorld"] += 1;
            counts["Hello"] += 1;
            counts["World"] += 1;
            counts["integer"] += 1;

        }

        Console.WriteLine("");

        foreach (KeyValuePair<string, int> item in counts)
        {
            Console.WriteLine(item.Key.ToString() + ":" + item.Value.ToString());
        }


        Console.ReadLine();
        }

E.g. Input: 1,10

Expected output:

hello: 3
world: 5
helloworld: 1
integer: 11

Run below dotnet fiddle to find issue:

https://dotnetfiddle.net/eKHb5x

RA19
  • 709
  • 7
  • 28

3 Answers3

1

You'll need to use if statements inside your foreach loop i.e.

foreach (var item in arraylist)
{
    if (item.Equals("Hello") )
    {
        counts["Hello"]++;
    }
    else if (item.Equals("World"))
    {
        counts["World"]++;
    }
    else if (item.Equals("HelloWorld"))
    {
        counts["HelloWorld"]++;
    }
    else if (item.Equals("integer"))
    {
        counts["integer"]++;
    }
}

Also, just a nitpick, you don't need

item.Key.ToString()

since the key is a string to begin with

item.Key

should suffice.

user8675309
  • 591
  • 1
  • 6
  • 24
1

Change your ArrayList to a List<string>.

When you iterate over it, use the item variable as the key to your dictionary.

        foreach(string item in list) {
            if(counts.ContainsKey(item)) {
                counts[item]++;
            } else {
                counts["integer"]++;
            }
        }
Trioj
  • 303
  • 2
  • 8
  • What's the benefit of converting to List? – user8675309 Nov 16 '17 at 20:00
  • https://stackoverflow.com/questions/2309694/arraylist-vs-list-in-c-sharp – Trioj Nov 16 '17 at 20:01
  • So instead of converting the entire collection, I could just swap your if for if d.ContainsKey(item.ToString())) for the same result? – user8675309 Nov 16 '17 at 20:04
  • Using a List vs calling item.ToString() are not remotely the same thing. You will achieve similar but less performant code. Whereas I have a list of objects that are already strings and do not need to be converted to strings, you have a list of objects that need to be converted to strings every time you need to do an operation on them and all the operations you do are string operations. – Trioj Nov 16 '17 at 20:04
0

Change your array list to a List and do this

foreach(var item in arrayList)
{
    if(counts.Contains(item))
    {
        counts[item]++;
    }
    else if (int.TryParse(item, out var i)) // this is a newish feature and my syntax might be a bit off
    {
        counts["integer"]++;
    }
}

I did this on my phone so sorry if there are any spelling mistakes. I'll check back shortly and correct then on my laptop

Dave
  • 2,829
  • 3
  • 17
  • 44