Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

如何找到一个数组中的众数?

输入一个含有n个元素的数组,统计出其中众数及其出现次数,若是有多个众数的情况如何统计?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
for(int i=0;i<a.length;i++){
int count=0;
  for(int j=0;j<a.length;j++){
    if(a[i]==a[j])
    count++;
    System.out.println(a[i]+" "+count);
}
}
//这是最暴力的方法了吧,等我回去在写吧

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...