如题,输出可能为多个。
C# codes as below,
/// <summary>
/// Get the max-time duplicated numbers in the param array
/// </summary>
/// <param name="array">the param array</param>
static void GetMaxDuplicatedNumbers(int[] array)
{
//Create a number container
int[] duplicateResult = new int[GetMaxElementInArray(array)-GetMinElementInArray(array)+1];
for (int i = 0; i < array.Length; i++)
{
duplicateResult[array[i]-GetMinElementInArray(array)]++;
}
int maxDuplicateTimes = GetMaxElementInArray(duplicateResult);
for (int i = 0; i < duplicateResult.Length; i++)
{
if (duplicateResult[i] == maxDuplicateTimes)
{
Console.WriteLine(i+GetMinElementInArray(array));
}
}
}
/// <summary>
/// Get the max element in the param array
/// </summary>
/// <param name="array">The param array</param>
/// <returns>The max element</returns>
static int GetMaxElementInArray(int[] array)
{
int result = array[0];
for (int i = 0; i < array.Length; i++)
{
if (result < array[i])
{
result = array[i];
}
}
return result;
}
/// <summary>
/// Get the min element in the param array
/// </summary>
/// <param name="array">The param array</param>
/// <returns>The min element</returns>
static int GetMinElementInArray(int[] array)
{
int result = array[0];
for (int i = 0; i < array.Length; i++)
{
if (result > array[i])
{
result = array[i];
}
}
return result;
}