+ 9

How to Find Multiple Modes From an Array or List of Numbers

I have here a program I finished last night (in C#): https://code.sololearn.com/c5Kg6l6WW1kK/#cs But when working on the mode of numbers entered, I could only find the mode of the number that repeats first in the sorted list. So I just left it out. Are there any algorithms out there that will work to find multiple modes from a list or array? Edit: Just an FYI or reminder, modes are the numbers repeated most in an assortment of numbers.

21st Aug 2017, 6:42 AM
Dalton Ponder
Dalton Ponder - avatar
10 Réponses
+ 4
Here is Version 2 of the code snippet I put together. This version has comments and the logic has been abstracted into a class with static types. - https://code.sololearn.com/c47B9x7afvOq/#cs If I continued working on this code, I would extend it to support Median, Mean, and Range to demonstrate how the rest of this would tie together. Let me know if you have any questions. Learning LINQ might feel awkward at first. However, there are some great tutorials and the absolute best way to learn is to experiment with a tool like LINQPad.
24th Aug 2017, 4:57 AM
David Carroll
David Carroll - avatar
+ 10
I wonder if I can store the first mode in a dictionary declaration, and then store more modes in the dictionary after iterating past the already stored modes. Anybody?
22nd Aug 2017, 4:42 PM
Dalton Ponder
Dalton Ponder - avatar
+ 8
What if I also use a list instead of an array, iterate all the numbers for the first mode, store the number in a dictionary, and then .Remove() that number from the list and iterate again to find more modes.
22nd Aug 2017, 4:52 PM
Dalton Ponder
Dalton Ponder - avatar
+ 7
One up'ed your comment. Maybe that'll help. When I can tonight, I'll paste the link to here to my code.
23rd Aug 2017, 11:21 PM
Dalton Ponder
Dalton Ponder - avatar
+ 6
Notes would be nice, since I'm newer to LINQ, but not necessary if it'll take a lot of your time. Thank you for doing this. I haven't had time to just sit down and code through and learn more about LINQ yet. So thank you, David.
24th Aug 2017, 3:39 AM
Dalton Ponder
Dalton Ponder - avatar
+ 6
Thanks, again, David. I will certainly look more into LINQ on YouTube and TutorialsPoint. I have DL'ed LINQPad and will learn how to use it as well.
24th Aug 2017, 5:00 AM
Dalton Ponder
Dalton Ponder - avatar
+ 3
I'm glad I could help. This is the first time I've actually responded to a code related question. I usually only take on challenges on a daily basis. However, this was an interesting question and I thought it would be fun.
24th Aug 2017, 5:03 AM
David Carroll
David Carroll - avatar
+ 2
I just realized I posted to a comment on your code and not in this post. I was having a hard time finding this question from the browser because this isn't appearing in my answered list. Hopefully, by passing an answer here (via my phone) I will be able to find this from my desktop browser.
23rd Aug 2017, 11:16 PM
David Carroll
David Carroll - avatar
+ 2
So... I took a stab at finding mode as a measure of center using LINQ and here is what I've got so far: - https://code.sololearn.com/c0uq69NRQFw1/#cs I will continue refining this to work similar to your code. Let me know if you'd like me to add comments explaining the various steps in my scratch pad. Also, I recommend using the Dump() in LINQ to take a look at how this code snippet works at various points.
24th Aug 2017, 3:36 AM
David Carroll
David Carroll - avatar
+ 2
No worries... Give me a few minutes to reorganize the code to be more readable. ;)
24th Aug 2017, 3:40 AM
David Carroll
David Carroll - avatar