Minimum Absolute Difference in an Array

  • + 0 comments

    java:

        public static int minimumAbsoluteDifference(List<Integer> arr) {
        int minimumAbsoluteDifference = Integer.MAX_VALUE;
        arr.sort(Comparator.naturalOrder());
        for (int i = 0; i < arr.size() -1; i++) {
            int absoluteDifference = Math.abs(arr.get(i) - arr.get(i + 1));
            if(absoluteDifference < minimumAbsoluteDifference){
                minimumAbsoluteDifference = absoluteDifference;
            }
        }
        return minimumAbsoluteDifference;
        }