Fastest way of finding the middle value of a triple?

前端 未结 25 960
庸人自扰
庸人自扰 2020-12-04 12:20

Given is an array of three numeric values and I\'d like to know the middle value of the three.

The question is, what is the fastest way of finding the midd

25条回答
  •  生来不讨喜
    2020-12-04 13:01

    A lot of these seem to be using pretty complex if statements. I've found a really simple workaround using the Math library.

    Math.max(Math.min(array[start], array[mid]), Math.min(array[start], array[mid], array[end]))
    

    Works out quite nicely.

提交回复
热议问题