using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
// sql如下,我把它翻译成代码,加深理解
// SELECT MAX(id)FROM sys_menu E1
// WHERE
// (SELECT COUNT(DISTINCT(E2.id)) FROM sys_menu E2
// WHERE E2.id > E1.id
// ) = 1; --9990
List<int> list = new List<int> { 1, 2, 3, 55, 66 };
int max = 0;
foreach (var id1 in list)
{
int count = 0;
var dic = new Dictionary<int, int>();
foreach (var id2 in list)
{
if (!dic.ContainsKey(id2))
{
if (id2> id1)
{
count++;
}
dic[id2] = 1;
}
}
//上面的代码说明id2>id1那么id1就不可能是最大的,并且比id1大的要求只能有一个,那么说过成语吧一人之下,万人之上 (注意这个至少要2个元素才有意义)
//所以也说明上面的sql有个陷阱SELECT MAX(id) 改为 select id也是可以的, 但其实max也是为了没有记录的时候返回一个空值
if (count != 1)
{
continue;
}
if (max == 0)
{
max = id1;
}
else if (id1> max)
{
max = id1;
}
}
Console.WriteLine(max);
Console.Read();
}
}
}