在arrayist中查找最受欢迎的字符串 [英] Finding most popular string in arrayist

查看:78
本文介绍了在arrayist中查找最受欢迎的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我正在尝试找到一种显示字符串的方法,该字符串在我的数组字符串列表中出现最多(即包含重复的条目)。此外,如果列表中有多个字符串具有高外观,即至少两个字符串具有多个条目(tie),则应显示两个/所有字符串。例如,如果字符串John出现的次数与字符串Mike一样多,则John和Mike都应显示为列表中最常用的名称,否则,如果只有一个字符串给出最高的条目,只显示该字符串。有没有办法我可以做到而不必先排序列表?



谢谢:)

解决方案

< blockquote>将列表重新处理为 System.Collections.Generic.Dictionary< string,Frequency> ,其中频率应该是一个由 uint 成员代表的频率的类(为简单起见,如果不应该是 struct ,那么整个想法是具有引用类型值)。第一个通用类型是键,第二个类型计算出现次数。字典不允许重复。所以,你按字符串键取键值对,如果没有找到它,加上频率为1,如果找到,加1到前一个频率值。



当你遍历所有输入列表时(再次,不是 StringList ,请看我对该问题的评论),你将把它重写到字典中,没有重复但他们的频率。要查看哪个字符串是冠军,请遍历字典。



此解决方案非常简单。



-SA

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 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


这篇关于在arrayist中查找最受欢迎的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆