using System; using System.Collections.Generic; using System.Text;namespace Collection { class Program { static void Main(string[] args) { int[] arr = initArr(); foreach (int i in arr) { Console.Write(i + ","); } Console.WriteLine(); Console.WriteLine("开始统计..."); //---------------------------- Dictionary<int, int> hashTable = new Dictionary<int, int>(); foreach (int i in arr) { if (!hashTable.ContainsKey(i)) hashTable[i] = 0; hashTable[i]++; } int maxNumber = 0; foreach (int i in hashTable.Keys) { if (hashTable[i] > maxNumber) maxNumber = hashTable[i]; //找到最大个数(hash表中的值) } foreach (int i in hashTable.Keys) { if (hashTable[i] == maxNumber) Console.WriteLine("数字{0},出现{1}次", i, hashTable[i]); } Console.ReadLine(); } private static int[] initArr() { List<int> list = new List<int>(); for (int i = 0; i < 3; i++) { list.Add(1); } for (int i = 0; i < 5; i++) { list.Add(3); } for (int i = 0; i < 7; i++) { list.Add(5); } for (int i = 0; i < 7; i++) { list.Add(6); } return list.ToArray(); } } }
using System;
using System.Collections.Generic;
using System.Text;namespace Collection
{
class Program
{
static void Main(string[] args)
{
int[] arr = initArr();
foreach (int i in arr)
{
Console.Write(i + ",");
}
Console.WriteLine();
Console.WriteLine("开始统计...");
//----------------------------
Dictionary<int, int> hashTable = new Dictionary<int, int>();
foreach (int i in arr)
{
if (!hashTable.ContainsKey(i))
hashTable[i] = 0; hashTable[i]++;
} int maxNumber = 0;
foreach (int i in hashTable.Keys)
{
if (hashTable[i] > maxNumber)
maxNumber = hashTable[i]; //找到最大个数(hash表中的值)
}
foreach (int i in hashTable.Keys)
{
if (hashTable[i] == maxNumber)
Console.WriteLine("数字{0},出现{1}次", i, hashTable[i]);
}
Console.ReadLine();
}
private static int[] initArr()
{
List<int> list = new List<int>();
for (int i = 0; i < 3; i++)
{
list.Add(1);
}
for (int i = 0; i < 5; i++)
{
list.Add(3);
}
for (int i = 0; i < 7; i++)
{
list.Add(5);
}
for (int i = 0; i < 7; i++)
{
list.Add(6);
}
return list.ToArray();
}
}
}