问题
The problem is that I have array with 5 numbers:
300 295 250 105 100 95
The result needed: the most numbers that have least difference specified by threshold. If you cant understand: in the example threshold is 5 and the winning set of numbers is 95,100,105 - because there are 3 numbers that are close to each other and the other set (295,300) is only 2.
I will try to come up with more clear explanation soon.
回答1:
I cannot give Javascript code, but I propose:
- Sort the list
- Compute sequential differences
- round or clip any values below
threshold
to zero, and all others to one - look for the longest continuous sequence of zeros (run-length encoding)
{95, 100, 105, 250, 295, 300} ---> {5, 5, 145, 45, 5} ---> {0, 0, 1, 1, 0}
来源:https://stackoverflow.com/questions/8165361/compare-difference-between-multiple-numbers