Skip to content

Instantly share code, notes, and snippets.

@arsane
Created July 16, 2015 11:27
Show Gist options
  • Save arsane/e26f68fdd81a84d9ebcb to your computer and use it in GitHub Desktop.
Save arsane/e26f68fdd81a84d9ebcb to your computer and use it in GitHub Desktop.
var arrays = [
[1, 4, 6, 123, 78, 8, 9, 124, 44],
[44, 6, 9, 123],
[124, 44, 16, 9, 123]
]
function findCommonElements(arrays) {
var sorted_arr = arrays.map(function(arr) {
// sort and remove duplicated elements in each array.
return arr.sort(function (a, b) {return a - b; }).reduce(function (r_arr, ele) {
if (r_arr.length == 0 || r_arr[r_arr.length-1] != ele) {
r_arr.push(ele);
}
return r_arr;
}, []);
}) //.sort(function(ra, rb) {return ra.length - rb.length; });
var res = []
var idxs = sorted_arr.map(function(arr) { return 0; })
sorted_arr.forEach(function(arr) { console.log(arr); })
var overflow = false;
var val = sorted_arr[0][0]
while(!overflow) {
// update indexs to get index for element >= max
console.log("Search:", val)
found_val = true;
for (var i = 0; i < sorted_arr.length; i++) {
// change to binary search shift?
var j;
for (j = idxs[i]; j < sorted_arr[i].length, sorted_arr[i][j] < val; j++) {}
console.log([i, j], "e", sorted_arr[i][j], "v", val);
if (j < sorted_arr[i].length) {
if (sorted_arr[i][j] > val) {
val = sorted_arr[i][j];
idxs[i] = j;
found_val = false;
break;
} else {
idxs[i] = j + 1;
// last element.
if (idxs[i] == sorted_arr[i].length) {
overflow = true;
}
}
} else {
overflow = true;
break;
}
}
if (found_val) { res.push(val);}
}
return res;
}
console.log("result:", findCommonElements(arrays))
//for nodejs run with: node.exe --harmony_generators
var arrays = [
[1, 4, 6, 123, 78, 8, 9, 124, 44],
[44, 6, 9, 123],
[124, 44, 16, 9, 123]
]
var check_all_exists = function (gens, val, count) {
var cur, gen;
for (var i = 0; i < count; i++) {
gen = gens.next().value;
while((cur = gen.next().value) != undefined && cur < val);
if (cur > val) {
gen.next(cur); // push back.
break;
}
}
return cur == val;
}
function findCommonElements(arr) {
var res = [];
var val;
var array_generators = arrays.map(function(arr) {
// sort and remove duplicated elements in each array.
return arr.sort(function (a, b) {return a - b; }).reduce(function (r_arr, ele) {
if (r_arr.length == 0 || r_arr[r_arr.length-1] != ele) {
r_arr.push(ele);
}
return r_arr;
}, []);
}).map(function (arr) {
// create generators for each array.
return function* () {
for (var i = 0; i < arr.length; i++) {
var a = yield arr[i];
if (a != undefined) { // deal with push back.
yield arr[i]; yield arr[i];
}
}
}();
});
// generator to iterate generators.
var generator_iterator = function* () {
var next = 0;
while(true) {
yield array_generators[next%array_generators.length]
next = next + 1;
}
}();
// iterate
while((val = generator_iterator.next().value.next().value) != undefined) {
console.log("Search:", val);
if (check_all_exists(generator_iterator, val, array_generators.length - 1)) {
console.log("\tfound:", val);
res.push(val);
}
}
return res;
}
console.log(findCommonElements(arrays))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment