How can I find the mode of a list of numbers? I know the logic of it (I think) but I don't know how to implement that logic or convert what my brain thinks into workable code.
This is what I know:
I need to have a loop that goes through the list one time to see how many times a number is repeated and an array to save the times a number is repeated. I also need to tell my program to discard the lesser amount once a larger one is found.
Yes, you are right:
Let we have a list of numbers:
You need to have a loop that goes through the list one time:
to see how many times a number is repeated in array to save the times a number is repeated:
Now, your dictionary
repetitions
stores how many (exactly value) times key value repeated.Then, you need to find the record of mode (i.e. record with the highest time of repetitions (i.e. highest value)) and take this one. LINQ will help us - let's sort the array by value and take the last one...or sort it descending and take the first one. Actually, that's the same in terms of result and productivity.
Here it is! Here we have a mode:
Your mode calculation logic is good. All you need is following your own instructions in a code :)