Skip to content

Instantly share code, notes, and snippets.

@shoyan
Created September 1, 2013 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoyan/6401840 to your computer and use it in GitHub Desktop.
Save shoyan/6401840 to your computer and use it in GitHub Desktop.
3つの配列から同じ数を見つけるアルゴリズムをJavaScriptで実装してみた。
/*
* 3つの配列から同じ数を見つける
*/
// 同じ数 31
var list1 = [2, 6, 10, 16, 24, 31, 38, 40, 50],
list2 = [1, 7, 11, 17, 25, 31, 39, 41, 51],
list3 = [3, 9, 13, 19, 27, 31, 32, 42, 52],
i = 0,
j = 0,
k = 0,
max = 1073741823;
list1.push(max);
list2.push(max);
list3.push(max);
while (list1[i] != list2[j] || list2[j] != list3[k]) {
if (list1[i] < list2[j]) {
i += 1;
} else if (list2[j] < list3[k]) {
j += 1;
} else {
k += 1;
}
}
if (list1[i] == max) {
console.log("一致する数はありませんでした");
} else {
console.log("一致する数は" + list1[i] + "です。");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment