java 计算数组中数字的出现次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11608515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
counting occurrence of numbers in array
提问by Doesn't Matter
I am stack for a while . I tried debugging but I couldn't figure out the solution. I am trying to count the occurrence of numbers. So my problem is that when I print the output it is
我堆栈了一段时间。我试过调试,但找不到解决方案。我正在尝试计算数字的出现次数。所以我的问题是当我打印输出时
3 occurs 1 times
1 occurs 1 times
0 occurs 1 times
2 occurs 1 times
1 occurs 2 times
3 occurs 2 times
2 occurs 2 times
0 occurs 2 times
10 occurs 1 times
4 occurs 1 times
instead of
代替
1 occurs 2 times
0 occurs 2 times
2 occurs 2 times
3 occurs 2 time
10 occurs 1 times
4 occurs 1 times
so if the number has more than 1 occurrence it should say it only once not as many times as there is occurrences. Cheers Here is the code
所以如果这个数字出现超过 1 次,它应该只说一次,而不是出现次数那么多。干杯这是代码
import java.util.*;
public class CountingOccuranceOfNumbers
{
public static void main(String[] args)
{
countNumbers();
}
public static void countNumbers()
{
Scanner input = new Scanner(System.in);
Random generator = new Random();
int[] list = new int[11];
int[] counts = new int[150];
int counter = 0;
int number = 1;
while(counter <= 10)
{
number = generator.nextInt(11);
list[counter] = number;
counter++;
}
for(int i=0; i<list.length - 1; i++)
{
counts[list[i]]++;
// System.out.print(list[i] + " ");
System.out.println(list[i] +" occurs " + counts[list[i]] + " times");
}
}
}
回答by cl-r
Use a HashMap<Integer>,<integer> ht
to manage your counts
使用 aHashMap<Integer>,<integer> ht
来管理您的计数
if (ht.get(newNumber) == null) {
ht.put(newNumber, 1);
} else {
ht.put(newNumber, ++ht.get(newNumber));
}
Corrected HashTable
in HashMap
and ++
before get(..)
HashTable
在HashMap
和++
之前更正get(..)
回答by Matt
Another option is guava's Multiset classes, which will track the count for you:
另一种选择是番石榴的 Multiset 类,它将为您跟踪计数:
int values[] = ...;
Multiset<Integer> ms = HashMultiset.create();
ms.addAll(Ints.asList(list));
int count0 = ms.count(Integer.valueOf(0));
int count1 = ms.count(Integer.valueOf(1));
Here, Multiset, HashMultiset and Ints are all guava classes.
这里的 Multiset、HashMultiset 和 Ints 都是番石榴类。
Note that Multiset does pretty much what someone mentioned above by using a Map and counter to track the counters. It's just abstracted away from you to make your code simpler.
请注意,Multiset 通过使用 Map 和计数器来跟踪计数器几乎可以完成上面提到的那些。它只是从您那里抽象出来,使您的代码更简单。
回答by Peter Lawrey
You have one loop to count the occurrence which also give a running total. It appears what you wanted is to only print the total when the counting is finish. i.e. it should be in another loop.
你有一个循环来计算出现次数,这也给出了一个运行总数。看来您想要的是仅在计数完成时打印总数。即它应该在另一个循环中。
回答by Keppil
Ok, I'll try to give you a hint or two.
好的,我会试着给你一两个提示。
- Since you get several lines printed for each number that occurs more than once, you probably shouldn't print anything until you are done with your counting.
- It looks like your output should be sorted after number of occurences. If this is the case, saving the counts in an array probably isn't the best idea. Consider using a
Map<Integer, Integer>
instead, where the key is the number, and the value is the number of occurences.
- 由于您会为每个出现多次的数字打印几行,因此在完成计数之前您可能不应该打印任何内容。
- 看起来您的输出应该在出现次数之后排序。如果是这种情况,将计数保存在数组中可能不是最好的主意。考虑使用 a
Map<Integer, Integer>
代替,其中键是数字,值是出现次数。
回答by user3061612
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class NumberRepetition {
public static void main(String[] args) throws Exception {
int size;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter size of array");
size=Integer.parseInt(br.readLine());
int el;
int[] a=new int[size];
for(int i=0;i<size;i++)
{
System.out.println("enter a number");
el=Integer.parseInt(br.readLine());
a[i]=el;
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
int count=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(a[i]==a[j])
{
count++;
}
}
System.out.println(a[i]+" \t " +"occurence time is"+"\t"+count);
if(count!=0)
{
i=i+count-1;
}
count=0;
}
}
}
回答by Wai Hein
Create a HashMap and put new Entry in the map with key,value where value is Integer.
创建一个 HashMap 并将新条目放入映射中,键,值,其中值是整数。
if you come across with same char, then increment the integer value associated with that key. o.w it is a new key and set the value to 1.
如果遇到相同的字符,则增加与该键关联的整数值。现在它是一个新键并将值设置为 1。
Integer entryValue;
Map map = new HashMap();
for ( int i =0; i < s1.length(); i++)
{
entryValue = (Integer)map.get(s1.charAt(i));
if (entryValue == null)
{
map.put(s1.charAt(i), new Integer(1));
}
else
{
map.put(s1.charAt(i), new Integer(entryValue.intValue()+1));
}
}