第二题答案:
public static void Func()
{
int[] arr = new int[] { 4, 3, 6, 8, 23, 4, 4, 5, 6, 9 };
Dictionary<int, int> d = new Dictionary<int, int>();
for (int i = 0; i < arr.Length; i++)
{
if (d.ContainsKey(arr[i]))
{
d[arr[i]]++;
}
else
{
d[arr[i]] = 1;
}
} int max = arr[0];
foreach (KeyValuePair<int, int> temp in d)
{
if (temp.Value > d[max])
{
max = temp.Key;
}
}
Console.WriteLine(max + " " + d[max]);
}
public static void Func()
{
int[] arr = new int[] { 4, 3, 6, 8, 23, 4, 4, 5, 6, 9 };
Dictionary<int, int> d = new Dictionary<int, int>();
for (int i = 0; i < arr.Length; i++)
{
if (d.ContainsKey(arr[i]))
{
d[arr[i]]++;
}
else
{
d[arr[i]] = 1;
}
} int max = arr[0];
foreach (KeyValuePair<int, int> temp in d)
{
if (temp.Value > d[max])
{
max = temp.Key;
}
}
Console.WriteLine(max + " " + d[max]);
}
public static void GetMax()
{
int[] arr = new int[] { 4, 3, 6, 8, 23, 4, 4, 5, 6, 9 };
int max = 0;
if (arr.Length >= 2)
{
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] - arr[i + 1] > max)
{
max = arr[i] - arr[i + 1];
}
}
}
Console.WriteLine(max);
}
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (int i in arr)
{
if (dic.ContainsKey(i))
dic[i] = dic[i] + 1;
else
dic.Add(i, 1);
}
var query =
from a in arr
group arr by a into g
orderby g.Count()
select new
{
g.Key,
count = g.Count()
};
//1.给出一个整数数组,求其中任意两个元素之差的最大值。
int[] arr = new int[] { 4, 3, 6, 8, 23, 4, 4, 5, 6, 9 };
var _之差的最大值=arr.Max()-arr.Min();
//2.给定一个整数数组,其中元素的取值范围为0到10000,求其中出现次数最多的数。
var _出现次数最多=arr.GroupBy(p=>p).Max(p=>p.Count());