Jump to content

Having trouble counting in C#...

samcool55

So, another day, another big mess.

 

I need to create a program, and one of it's functons it needs to have is to count how many times a name appears in a list.

Now, i can't use LINQ, or anything else that makes it easy. I'm quite limited...

 

Yes i tried google, but the fact i can't use LINQ reduces the amount of useable options to 0...

Probably not the right way, but it's just to give you an idea what i want, sort of.

 

List<string> names = new List<string>();foreach(string name in names){    if("Something that checks how many times the name is in the list")}

If you want my attention, quote meh! D: or just stick an @samcool55 in your post :3

Spying on everyone to fight against terrorism is like shooting a mosquito with a cannon

Link to comment
Share on other sites

Link to post
Share on other sites

First of all, please use code tags. You can add them in by using the <> icon above where you type your posts, or with your text like so

[code]Code goes here[/code]
Second, what's the issue? You almost have it done. Compare the names in the if statement and increment a variable by 1 if it's true.
Link to comment
Share on other sites

Link to post
Share on other sites

First of all, please use code tags. You can add them in by using the <> icon above where you type your posts, or with your text like so

[code]Code goes here[/code]
Second, what's the issue? You almost have it done. Compare the names in the if statement and increment a variable by 1 if it's true.

 

Yes, almost, that's the problem, i have no idea how to do the last part, it's probably really easy but for some weird reason i can't figure it out...

If you want my attention, quote meh! D: or just stick an @samcool55 in your post :3

Spying on everyone to fight against terrorism is like shooting a mosquito with a cannon

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, almost, that's the problem, i have no idea how to do the last part, it's probably really easy but for some weird reason i can't figure it out...

 

Unless I'm misunderstanding the question, it would just be this.

int count = 0foreach(string name in names){    if(nameToCount == name)    {        count++;    }}
Link to comment
Share on other sites

Link to post
Share on other sites

@samcool55 Are you trying to find how many times a given name appears or do you want to find out how many times every name appears (frequency count of words)?

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×