LINQ to find the closest number that is greater / less than an input

后端 未结 8 639
猫巷女王i
猫巷女王i 2021-02-05 19:57

Suppose I have this number list:

List = new List(){3,5,8,11,12,13,14,21}

Suppose that I want to get the closest number th

8条回答
  •  广开言路
    2021-02-05 20:44

    Use Array.BinarySearch - no need for LINQ or visiting on average half the elements to find your target.

    There are also a variety of SortedXXX classes that may be suitable for what you're doing [that will have such efficient O(log N) searches built-in]

提交回复
热议问题