JSFiddle - React, Tailwind, and code Playground
by podlipensky
HTML
You were given with array of elements [0..n-1]. All elements are distinct except two of them. Find repeated elements without additional array and for O(n) time
JavaScript
var a =[1,2,6,4,5,6,7,8,9,0];
var i = 0, n = a.length, r;
while(i < n){
var j = a[i] % n;
a[j] += n;
if(a[j] >= 2 * n){
r = j;
console.log('found!');
break;
}
i++;
}
console.log(r);