-4

I wrote the below code to solve a prep test, found online. The goal is to find the min absolute difference between numbers in an array.

function minimumAbsoluteDifference(arr: number[]): number {
    // Write your code here
    let res = 0
    const length = arr.length
    let diff : number
    for (let i = 0; i < length; i++) {
        for (let j = 0; j < length; j++) {
            if (i !== j) {
                diff = Math.abs(arr[i] - arr[j])
                if (diff < res || res === 0)
                  res = diff
            }            
        }
    }
    return res
}

i ran tests on Math.abs(), and these days it is faster than a bitshift version... It seems that the only way to make this faster, is either with a reduce, or create a hash to optimize the nested array version.

Any ideas how to rewrite this with reduce()?

5
  • 2
    If your main goal is to improve performance, then: for (let j = i+1; Unclear if reduce() will improve performance or just reduce the amount of code.
    – fdomn-m
    Commented Mar 26 at 17:30
  • 4
    SO is not a code-writing service. Please make your own attempt to solve it, and we'll help you fix it if you get it wrong. As you're doing this as an educational exercise, that's the best way to learn.
    – Barmar
    Commented Mar 26 at 17:31
  • 2
    Stack Overflow is not a forum. You should reread the tour.
    – jabaa
    Commented Mar 26 at 18:11
  • This platform is community-driven. The community gave your question and your answer 6 downvotes, 3 close votes, no upvotes, no reopen votes and explained you in the comments, what's wrong with the question and the answer. That's how this community works. If you want to learn more, you can read the tour, How to Ask and How to Answer.
    – jabaa
    Commented Mar 26 at 20:13
  • 1
    I think the problem here is a misunderstanding. The goal of this platform is not to help you. The goal of this platform is to collect questions and answers to help future users with similar questions, but this only works if the questions have a specific quality and the answers answer the actual question. If you don't want to improve your posts, it's more helpful for future users if the posts are removed.
    – jabaa
    Commented Mar 26 at 20:31

1 Answer 1

-2

Actually, no reduce is needed. I figured this one out.

The point here, is that for the difference to be minimal, the numbers have to be close. Therefore, we just need to sort the array, then do a for loop with:

diff = Math.min(Math.abs(sorted[i] - sorted[i - 1]), diff)

This removed the need to run the nested loop, and solved the performance issue.

3
  • 2
    Sorting an array has O(n log n) time complexity. That's the same as two nested loops where the second one uses (let j = i + 1;
    – James
    Commented Mar 26 at 17:59
  • 1
    The question is "how to rewrite this with reduce()?" I don't see, how this answer answers the question.
    – jabaa
    Commented Mar 26 at 18:08
  • @James i believe that v8 uses timesort, which has a lower complexity in the sense that O(n log n) would be the worst case. Commented Mar 27 at 15:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.