如何从字符串中找出出现次数最多的数字?
我有一个 String
,其中包含以逗号分隔的形式的数字。我想从字符串中提取出现次数最多的数字。 例如,我有一个字符串,
String str="1,2,3,4,5,6,7,19,18,4";
例如上面的 str,我需要 4,因为 4 是 str 的两倍。
相同
String str2="1,2,3,4,6,4,3,9";
与上面的 str2
,我需要 3,4如果所有数字都是唯一的,那么我需要第一个。
建议我更好的方法。
I have a String
which contains numbers in a comma separated form. and I want to extract the most appears numbers from the String.
For example I have a String like..
String str="1,2,3,4,5,6,7,19,18,4";
from the above str i need 4 because 4 is two times in str.
same as
String str2="1,2,3,4,6,4,3,9";
from the above str2 i need 3,4
In case of all numbers are unique then i need first one.
suggest me better approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
java.util.StringTokenizer
使用,
作为分隔符来标记字符串,然后使用String.trim( 从每个标记中去除前导和尾随空格)
,然后使用Integer.parseInt()
函数将它们存储到int
数组中。然后你就可以轻松地数出它们了。用于计数:
如果您的数字范围较小,那么您可以简单地创建一个计数器数组并使用每个数字作为该数组的索引。如果没有,那么您可以使用 HashMap 或类似的东西。
You can tokenize the string using
java.util.StringTokenizer
using,
as the separator, then strip off the leading and trailing spaces from each token usingString.trim()
, then store them into an array ofint
s usingInteger.parseInt()
function. Then you can count them easily.For counting:
If you have a small range of numbers, then you can simply create a counter array and use each number as an index to that array. If not, then you can use
HashMap
or something like that.假设这是一项作业,我将建议一个没有代码的一般想法:
','
字符上拆分字符串Integer
的初始空映射将计数转换为Integer
int
值1
的条目。1
,则返回第一个令牌1
,则转到再次遍历映射,并收集值等于 max 的所有键。这种方法可以让您处理非常大的数字,并且它不会区分带或不带前导零的数字。换句话说,它将把
1,2,03,3,3,2
序列中的3
识别为唯一的获胜者,而不是与2< /代码>。
Assuming that this is a homework, I'll suggest a general idea without the code:
','
charactersInteger
toInteger
with the countsint
values1
.1
, return the first token1
, go through the map again, and collect all keys where the value is equal to maxThis approach lets you work with very large numbers, and it does not discriminate between numbers with and without leading zeros. In other words, it will identify
3
in1,2,03,3,3,2
sequence as a unique winner, not as a tie with2
.我将有一个整数映射数组来计算每个数字,以及一个变量来存储具有最高计数的数字。
将字符串拆分为数字列表,然后循环该列表。
每次查看新数字时,都会增加其在地图中的计数器,如果该计数大于当前最高计数,则将当前最高计数更改为当前计数。在字符串末尾,返回当前最高值。
请注意,如果您还存储第二大计数的数字,则可以通过提前退出循环来进行稍微优化,此时没有足够的数字允许第二大计数超过最高计数。
I would have an array of map of integers to count each number, and variable to store the number with the highest count.
Split the string into a list of numbers, and loop over the list.
Each time you look at a new number, increment its counter in the map, and if that count is greater than the current highest count, change the current highest to the current. At the end of the string, return the current highest.
Note that, if you also store the number with the second biggest count, you can optimise slightly by exiting the loop early, when there are not enough numbers left to allow the second highest count to exceed the highest.
将字符串转换为数字列表。
记住列表的第一个数字是什么。
对数字列表进行排序。
迭代列表并保留
在每次迭代中,如果当前数字等于当前最大值,则将当前数字添加到结果。如果大于当前最大值,则清除结果集并将当前数字添加到结果中。
在循环结束时,如果结果集的大小等于不同数字的数量,则返回对列表进行排序之前记住的第一个数字。
Transform your String into a list of numbers.
Remember what the first number of the list is.
Sort the list of numbers.
Iterate over the list and keep
At each iteration, if the current number is equal to the current max, add the current number to the result. If it's greater than the current max, clear the set of results and add the current number to the results.
At the end of the loop, if the size of the result set is equal to the number of distinct numbers, return the first number you remembered before sorting the list.
尝试下面的代码
下面将输出
如果有任何疑问请告诉我......
干杯!
Try Below Code
Below will be output
Let me know if there are any queries...
Cheers!!!
这是使用 java.util 的解决方案和 番石榴收集。
大纲:
Word -> Count
Count -> Words
代码:
Here's a solution using java.util and Guava collect.
Outline:
Word -> Count
Count -> Words
Code: