Compare difference between multiple numbers

为君一笑 提交于 2019-12-25 07:54:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!