Skip to content

1200. Minimum Absolute Difference

https://leetcode.com/problems/minimum-absolute-difference/submissions/

js
/**
 * @param {number[]} arr
 * @return {number[][]}
 */
var minimumAbsDifference = function(arr) {
    arr = arr.sort((a, b) => a - b)
    const diffs = arr.reduce((ret, val, i) => {
        if (i < arr.length - 1) {
            ret.push(arr[i + 1] - val)
        }
        return ret
    }, [])
    const minDiff = Math.min(...diffs)
    return diffs.reduce((ret, diff, i) => {
        if (diff === minDiff) {
            ret.push([arr[i], arr[i + 1]])
        }
        return ret
    }, [])
};