If I understand you correctly, then the following will do what you want.
function threeDistincts(items) {
var distincts = 0, inArr = false;
for (var i = 0; i < items[0].length; i++) {
inArr = false;
for (var j = 0; j < i; j++) {
if (items[0][j] === items[0][i]) {
inArr = true;
break;
}
}
if (!inArr) distincts++;
if (distincts === 4) break;
}
items[0].length = items[1].length = i;
}
Call it like:
var items = [[1,1,2,2,3,3,3,4,5,5],[0,0,0,1,0,1,0,1,0,1]];
threeDistincts(items);
// items = [[1,1,2,2,3,3],[0,0,0,1,0,1]];
Working Fiddle.
Another version of this function, using indexOf
(as DTing suggested), will limit the inner loop (in the original function) to the distinct elements only, but considering the high complexity of Array.indexOf
and Array.push
implementation, better performance is not guaranteed.
function threeDistincts(items) {
var arr = [];
for (var i = 0; i < items[0].length; i++) {
if (arr.indexOf(items[0][i]) === -1) arr.push(items[0][i]);
if (arr.length === 4) break;
}
items[0].length = items[1].length = i;
}