Finding out the minimum difference between elements in an array

前端 未结 8 639
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 01:02

I have an integer array with some finite number of values. My job is to find the minimum difference between any two elements in the array.

Consider that the array co

8条回答
  •  既然无缘
    2020-12-08 01:17

    sharing the simplest solution.

    function FindMin(arr) {
    
        //sort the array in increasing order
    
        arr.sort((a,b) => {
           return a-b;
        });
    
        let min = arr[1]-arr[0];
    
        let n = arr.length;
    
        for (var i=0;i

提交回复
热议问题