using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main()
{
var test1 = new[] { 1, 2, 2, 3, 0, 1, 3, 0, 2, 1 };
var test2 = new[] { 1, 2, 3, 0, 1, 3, 0, 2 };
Console.WriteLine(string.Join(",", UniqueRepetitiveSort(test1)));
Console.WriteLine(string.Join(",", UniqueRepetitiveSort(test2)));
}
public static ICollection<int> UniqueRepetitiveSort(ICollection<int> source)
{
var dicCount = source.GroupBy(s => s).ToDictionary(g => g.Key, g => g.Count());//得到一个以元素为键值,元素个数为值得字典
var result = new List<int>();
//字典还有值时
while (dicCount.Any())
{//按照数字大小排序
foreach (var kv in dicCount.Select(kv => kv).OrderBy(kv => kv.Key))
{
result.Add(kv.Key);
if (--dicCount[kv.Key] == 0)//移除元素个数为1的键
{
dicCount.Remove(kv.Key);
}
}
}
return result;
}
}
}
结果如图