给定一个整数数组,其中元素的取值范围为0到10000,求其中出现次数最多的数

hehuan1213 2009-08-06 06:11:48
给定一个整数数组,其中元素的取值范围为0到10000,求其中出现次数最多的数
...全文
591 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hecker728 2009-08-07
  • 打赏
  • 举报
回复
排序花的时间比较多
licrosoftyzb 2009-08-07
  • 打赏
  • 举报
回复
帮2楼的同志贴个完整代码
using System;

namespace IntegerArrayTest
{
class Program
{
public static void Main(string[] args)
{
int[] i= new int[] {1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,9,9,9,9,9,9,9,9};
int[] count = new int[10001];
int maxCount=0;
int maxResult=0;
foreach(int index in i)//遍历i数组
{
count[index+1]+=1;
}
for(int index =0;index<10000;index++)//遍历count数组
{
if(count[index+1]>maxCount)
{
maxCount=count[index+1];
maxResult=index;
}
}
//输出
Console.Write("出现最多的数字为{0},出现次数为{1}.",maxResult,maxCount);
Console.Write("\nPress any key to continue . . . ");
Console.ReadKey(true);
}
}
}
  • 打赏
  • 举报
回复

对于数的数组里的数数目多的,用二楼方法。
对于数组里的数数目较少的,用4楼方法。
都不错。
yutian_31 2009-08-06
  • 打赏
  • 举报
回复
up
SQL77 2009-08-06
  • 打赏
  • 举报
回复
                int [] my=new int []{1,2,3,4,56,1,1,1,5,4};
string str = "";
foreach (int i in my)
{
str=str+i.ToString ()+",";
}
str = str.TrimEnd(',');
//Console.Write (str);
MatchCollection mac = Regex.Matches(str, @"\d*,");
int num=0;
List<int> a = new List<int>();
foreach (Match m in mac)
{
num= Regex.Matches(str, m.Value).Count;
str = Regex.Replace(str, m.Value, "");
a. Add(num);
}
a.Sort();


Console.WriteLine(a[a.Count-1]);
pinyu 2009-08-06
  • 打赏
  • 举报
回复
还不如先sort
text.sort
dim max,currCount as integer
max=0
currCount=1
for i=1 to text.length-1
if a(i)=a(i-1) then
currCount += 1
else
if max<current then
max=current
current=1
end if
end if
hecker728 2009-08-06
  • 打赏
  • 举报
回复
给定的数组 text[10001]

定义一个数组int[] a=new a[10001]

遍历text,得到数值多少就把a相应索引位的值加1.

遍历完text后再遍历a

int result=0,time=0;
for(int i=0;i<a.length;i++)
{
if(a[i]<a[i+1])
{
result+=1;
time=i+1;
}else{
a[i+1]=a[i];}
}

result就是出现次数最多的数
time是出现的次数
dancingbit 2009-08-06
  • 打赏
  • 举报
回复
遍历,统计。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧