在arrayist中查找最受欢迎的字符串 [英] Finding most popular string in arrayist
问题描述
谢谢:)
< blockquote>将列表重新处理为 System.Collections.Generic.Dictionary< string,Frequency>
,其中频率
应该是一个由 uint
成员代表的频率的类(为简单起见,如果不应该是 struct
,那么整个想法是具有引用类型值)。第一个通用类型是键,第二个类型计算出现次数。字典不允许重复。所以,你按字符串键取键值对,如果没有找到它,加上频率为1,如果找到,加1到前一个频率值。
当你遍历所有输入列表时(再次,不是 StringList
,请看我对该问题的评论),你将把它重写到字典中,没有重复但他们的频率。要查看哪个字符串是冠军,请遍历字典。
此解决方案非常简单。
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 :)
Re-work the list intoSystem.Collections.Generic.Dictionary<string, Frequency>
, whereFrequency
should be a class with the frequency represented by auint
member (for simplicity, if should not bestruct
, 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, notStringList
, 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
这篇关于在arrayist中查找最受欢迎的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!