MYYA
您可以对数组进行排序,然后运行它,然后查看下一个(或前一个)索引是否与当前索引相同。假设您的排序算法很好,这应该小于O(N)2):var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];var sorted_arr = arr.slice().sort(); // You can define the comparing function here. // JS by default uses a crappy string compare. // (we use slice to clone the array so the // original array won't be modified)var results = [];for (var i = 0; i < sorted_arr.length - 1; i++) { if (sorted_arr[i + 1] == sorted_arr[i]) { results.push(sorted_arr[i]); }}console.log(results);