I am trying to increase the efficiency of a function. It is currently quadratic and I would like to make it logarithmic.
The third to last line of the current function is confusing me somewhat as well and I would like some clarification.
function solution(arr){
let result = 0
for ( let i = 0; i < arr.length; i++)
for (let j = 0; j < arr.length; j++)
if (arr[i] == arr[j])
result = Math.max(result, Math.abs(i - j));
return result;
}
How do I solve this problem?