2 回答

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個贊
您可以按值對數(shù)據(jù)進(jìn)行分組,對組進(jìn)行排序,然后在記住計(jì)數(shù)的情況下迭代組 - 每次遞減并在達(dá)到零時刪除事物,或者增加計(jì)數(shù)器并僅輸出至少人口眾多的事物。就像是:
var values = new[] { 5, 2, 2, 1, 3, 3, 4 };
var data = new SortedDictionary<int, int>();
foreach(var val in values)
{
int count;
if (!data.TryGetValue(val, out count)) count = 0;
data[val] = count + 1;
}
int lim = 0;
bool any;
do
{
any = false;
foreach (var pair in data)
if (pair.Value > lim)
{
Console.WriteLine(pair.Key);
any = true;
}
lim++;
} while (any);
- 2 回答
- 0 關(guān)注
- 199 瀏覽
添加回答
舉報(bào)