I have two arrays:
arr1 = [0,1,1,0]
arr2 = [a,b,c,d]
I would like to find the values and corresponding indices of arr2[ i ] where i is such that arr1[ i ] != 0 without looping through each position in arr2. What efficient techniques could do something like the following:
arr2.forEach( ( 'element with index such that arr1[index] != 0') => {} );
** EDIT ** The initial posting of this question wasn't clear about needing to record the indices of the elements that met the condition.
for-loop overarr1...for(...) if (arr1[i] === 0) doSomething(arr2[i])O(n log n)followed by a binary search ofO(log n), whereas a loop isO(n). I'm not saying your suggestion is wrong, just that it would require very specific circumstances for that to be the most efficient.