我怎样才能比较两组1000个号码？

``foreach( \$numbers1 as \$n1 ) { foreach( \$numbers2 as \$n2 ) { if( \$n1 == \$n2 ) { doBla(); } } }` `

` `var A = getFirstArray().sort(), B = getSecondArray().sort(); var i = 0, j = 0; while (i < A.length && j < B.length) { if (A[i] === B[j]) { doBla(A[i]); i++; j++; } else if (A[i] < B[j]) { i++; } else j++; }` `

（这是JavaScript;你也可以在服务器端做，但我不知道PHP。）

` `var map = {}; for (var i = 0; i < B.length; ++i) map[B[i]] = true; // Assume integers. for (var i = 0; i < A.length; ++i) if (map[A[i]]) doBla(A[i]);` `

` `var map = {}; for (var i = 0; i < B.length; ++i) map['' + B[i]] = true; // Assume integers. for (var i = 0; i < A.length; ++i) if (map['' + A[i]]) doBla(A[i]);` `

• `array_intersect()`返回匹配
• `array_diff()`返回差异

` `select x from table1 inner join table2 on table1.x = table2.y` `

` `\$result = array_intersect(\$numbers1, \$numbers2); foreach (\$result as \$val) doBla();` `

` `sort(\$numbers2); foreach(\$numbers1 as \$n1) { if (BinarySearch(\$numbers2, \$n1) >= 0) { doBla(); } }` `

` `// Load one array into a hashtable, keyed by the number: O(n). \$keys1 = []; foreach(\$numbers1 as \$n1) \$keys1[\$n1] = true; // Find the intersections with the other array: foreach(\$numbers2 as \$n2) { // O(n) if (isset(\$keys1[\$n2]) { // O(1) doBla(); } }` `

` `\$same_numbers = array_intersect(\$numbers1, \$\$numbers2); foreach(\$same_numbers as \$n) { doBla(); }` `

` `<?php // Fill two arrays with random numbers as proof. \$first_array = array(1000); \$second_array = array(1000); for(\$i=0; \$i<1000; \$i++) \$first_array[\$i] = rand(0, 1000); for(\$i=0; \$i<1000; \$i++) \$second_array[\$i] = rand(0, 1000); // The loop you care about. for(\$i=0; \$i<1000; \$i++) if (\$first_array[\$i] != \$second_array[\$i]) echo "Error at \$i: first_array was {\$first_array[\$i]}, second was {\$second_array[\$i]}<br>"; ?>` `

` `<?php // Fill two arrays with random numbers as proof. \$first_array = array(1000); \$second_array = array(1000); for(\$i=0; \$i<1000; \$i++) \$first_array[\$i] = rand(0, 1000); for(\$i=0; \$i<1000; \$i++) \$second_array[\$i] = rand(0, 1000); \$matches = array_intersect(\$first_array, \$second_array); \$differences = array_diff(\$first_array, \$second_array); ?>` `

foreach \$ e（@a，@b）{\$ union {\$ e} ++ && \$ isect {\$ e} ++}

@union =键％union; @isect =键％isect;

` `\$results = array_intersect(\$numbers1, \$numbers2); foreach(\$results as \$rk => \$rv) { doSomething(\$rv); }` `

` `// 1. Create a hash map from one of the lists. var hm = { }; for (var i in list1) { if (!hm[list1[i]]) { hm[list1[i]] = 1; } else { hm[list1[i]] += 1; } } // 2. Lookup each element in the other list. for (var i in list2) { if (hm[list2[i]] >= 1) { for (var j = 0; j < hm[list2[i]]; ++j) { doBla(); } } }` `

` `Mergesort (List A); Mergesort (list B) \$Apos = 0; \$Bpos = 0; while( \$Apos != A.Length && \$Bpos != B.length) // while you have not reached the end of either list { if (A[\$Apos] == B[\$Bpos])// found a match doSomething(); else if (A[\$Apos] > B[\$Bpos]) // B is lower than A, so have B try and catch up to A. \$Bpos++; else if (A[\$Apos] < B[\$Bpos]) // the value at A is less than the value at B, so increment B \$Apos++; }` `

1. 创build两个重复的集合，最好是具有快速查找时间的集合，比如HashSet或者TreeSet。 避免列表，因为他们有很差的查找时间。

2. 当你find元素，从两个集合中删除它们。 这可以通过在稍后的search中筛选更less的元素来减less查找时间。

` `\$unique = array(); foreach (\$list1 as \$num) { \$unique[\$num] = \$num; } foreach (\$list2 as \$num) { \$unique[\$num] = \$num; } \$unique = array_keys(\$unique);` `

` `// get [val => occurences, ...] for \$numbers2 \$counts = array_count_values(\$numbers2); foreach (\$numbers1 as \$n1) { // if \$n1 occurs in \$numbers2... if (isset(\$counts[\$n1])) { // call doBla() once for each occurence for (\$i=0; \$i < \$counts[\$n1]; \$i++) { doBla(); } } }` `

` `foreach (\$numbers1 as \$n1) { if (in_array(\$n1, \$numbers2)) doBla(); }` `

` `\$dups = array_intersect(\$numbers1, \$numbers2); foreach (\$dups as \$n) doBla();` `

` `function getWayStr(curr) { var nextAbove = -1; for (var i = curr + 1; i < waypoints.length; ++i) { if (nextAbove == -1) { nextAbove = i; } else { wayStr.push(waypoints[i]); wayStr.push(waypoints[curr]); } } if (nextAbove != -1) { wayStr.push(waypoints[nextAbove]); getWayStr(nextAbove); wayStr.push(waypoints[curr]); } }` `

合并，sorting，然后数

` `<?php \$first = array('1001', '1002', '1003', '1004', '1005'); \$second = array('1002', '1003', '1004', '1005', '1006'); \$merged = array_merge(\$first, \$first, \$second); sort(\$merged); print_r(array_count_values(\$merged)); ?>` `

输出/数值为三是你想要的

` `Array ( [1001] => 2 [1002] => 3 [1003] => 3 [1004] => 3 [1005] => 3 [1006] => 1 )` `