Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Jan 1, 2024
1 parent 09aefd6 commit e23bf45
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function smallestDifference(arrayOne, arrayTwo) {
let output = [];
let indexOne = 0;
let indexTwo = 0;
let smallestDistance = Infinity;

arrayOne.sort((a, b) => a - b);
arrayTwo.sort((a, b) => a - b);

while (indexOne < arrayOne.length && indexTwo < arrayTwo.length) {
let one = arrayOne[indexOne];
let two = arrayTwo[indexTwo];
let distance = Math.abs(one - two);

if (distance < smallestDistance) {
smallestDistance = distance;
output = [one, two];
}

if (one < two) indexOne++;
else indexTwo++;
}

return output;
}

0 comments on commit e23bf45

Please sign in to comment.