如何求出数组中出现次数最多的数字(C#实现)

[复制链接]
查看11 | 回复5 | 2009-8-13 09:39:23 | 显示全部楼层 |阅读模式
///
/// 求出数组中出现次数最多的数字
///
public class MaxCounter
{
///
/// numbers:要统计的数组,count:统计最多资料
/// 返回出现次数最多的数字
///
public int GetMaxCounts(int[] numbers, out int count)
{
Hashtable _hash = new Hashtable();...
回复

使用道具 举报

千问 | 2009-8-13 09:39:23 | 显示全部楼层
已经测试过的:
int[] A = new int[9] { 1, 5, 2, 1, 2, 6, 5, 2, 5 };
int[] B = new int[A.Length]; //记录每个数字重复的次数
int[] max = new int[A.Length]; //记录出现次数最...
回复

使用道具 举报

千问 | 2009-8-13 09:39:23 | 显示全部楼层
代码在txt文档中编写,有错误处自行修改下。int num = new int[]{1,2,3,1};int num2 = new int[num.length];int b = 0;for(int i = 0; i < num.length;i++){int a = 0;for(int j = 0; j < ...
回复

使用道具 举报

千问 | 2009-8-13 09:39:23 | 显示全部楼层
int num = new int[]{1,2,3,1};int num2 = new int[num.length];int b = 0;for(int i = 0; i < num.length;i++){int a = 0;for(int j = 0; j < num.length;j++){if(num[i...
回复

使用道具 举报

千问 | 2009-8-13 09:39:23 | 显示全部楼层
这种题目用linq更好些,虽然我还没有开始接触linq,但是我按着示例给你写了一个,不要忘记引入using System.Linq:
int maxCountNum=(
from num in new[] { 1, 12, 12, 21, 12, 4, 4, 5 }
group...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行