嗨,我需要根据给定范围在值列表中找到最大的密集区域
例子:
var radius =5; //Therm edited
var list = new List<int>{0,1,2,3,4,5,12,15,16,22,23,24,26,27,28,29};
//the following dense regions exist in the list above
var region1 = new List<int> { 0, 1, 2, 3, 4, 5 }; // exist 6 times (0, 1, 2, 3, 4, 5)
var region2 = new List<int> { 12, 15, 16}; // exist 3 times (12, 15, 16)
var region3 = new List<int> { 22, 23, 24, 26, 27}; // exist 1 times (22)
var region4 = new List<int> { 22, 23, 24, 26, 27, 28}; // exist 1 times (23)
var region5 = new List<int> { 22, 23, 24, 26, 27, 28, 29 }; // exist 3 times (24, 26, 27)
var region6 = new List<int> { 23, 24, 26, 27, 28, 29 }; // exist 1 times (28)
var region7 = new List<int> { 24, 26, 27, 28, 29 }; // exist 1 times (29)
//var result{22,23,24,26,27,28,29}
解决方案并不需要很快,因为值的最大数量是 21 有没有办法使用 fluent 来实现这一点?
我只知道如何获得最接近的值
int closest = list.Aggregate((x,y) => Math.Abs(x-number) < Math.Abs(y-number) ? x : y);
以及如何获取两个数字之间的值
var between = list.Where(value=> min < value && value < max);
编辑
附加信息
好的范围可能是错误的热半径会是一个更好的词。
我将密集区域定义为 currenvalue-range 和 currenvalue + range 之间所有值的最大计数,我们得到密集区域