I have an array of integers, lets say const foo = [2,5,6,2...]
I would like to create a function that compare all the items and return true if one item is exactly the double of any other, and false if no such case is found.
First idea I had is to do something like
function foo(arr) {
for(let ind = 0; ind < arr.length; ind++) {
for( j = 1; j < arr.length; j++) {
/// CHECK IF ITEM arr[ind] is double of arr[j] or viceversa and return true
}
}
return false
}
This is obviously a pretty bad solution as it goes through each item multiple times. What is the most efficient way for solving this in JS?