Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys. I'm trying to find a way of displaying a string that appears the most in my array-list of strings, (i.e. contains repeated entries). Also, if more than one string has a high appearance in the list, i.e. at least two strings have multiple entries (ties), both/all strings should be displayed. for example, if the string "John" appears as many times as the string, "Mike", both "John" and "Mike" should be displayed as the most popular names in the list, else, if only one of the strings gives the highest entry, only that string should be displayed. Is there a way I can do it without having to sort the list first?

Thanks :)
Posted
Updated 5-Mar-13 7:28am
v3
Comments
Sergey Alexandrovich Kryukov 4-Mar-13 17:28pm    
First of all, do yourself a favor: forget ArrayList, it's obsolete; use System.Collections.Generic.List<string>. And what's the problem? What do you try? This is a very simple things, so what?
—SA
Thomas Daniels 5-Mar-13 13:40pm    
Please don't fill your question with "jjkjn jkj j j k kj kj j k j j kj kj k" or something similar. I clicked on the "Rollback" button, so the current version of your question is the same as the first version of the question.

1 solution

Re-work the list into System.Collections.Generic.Dictionary<string, Frequency>, where Frequency should be a class with the frequency represented by a uint member (for simplicity, if should not be struct, the whole idea is to have a reference-type value). First generic type is the key, the second one counts the number of occurences. A dictionary does not allow duplicates. So, you take the key-value pair by string key, if it is not found add it with frequency 1, if it is found, add 1 to previous frequency value.

When you traverse all the input list (again, not StringList, please see my comment to the question), you will have it re-written to the dictionary, without duplicates but with their frequencies. To see which string is the champion, traverse the dictionary.

This solution is quite simple.

—SA
 
Share this answer
 
v2
Comments
raphael mat 5-Mar-13 5:42am    
Sorry didn't know this site was for advanced programmers. Wrong site!!!!
Sergey Alexandrovich Kryukov 5-Mar-13 11:31am    
For your information, this is the beginner's level of knowledge, very basic use of some well-known ready-to-use classes.
Below this level is only not doing programming at all.
—SA
raphael mat 5-Mar-13 13:23pm    
ok, cool

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900