简体   繁体   English

返回重复数字的数组

[英]Return an array of the repeated numbers

I have a sorted array and i want to return an array of just the repeated numbers array1 = [1,2,2,3,4,5,5,5,6,6,6,6] the output would be [2,5,6] what am i missing?我有一个排序数组,我想返回一个仅包含重复数字的数组 array1 = [1,2,2,3,4,5,5,5,6,6,6,6] output 将是 [2 ,5,6] 我错过了什么? i used javascript but you can use java too if you prefer我使用了 javascript 但如果您愿意,也可以使用 java

var findRepeated = function(numbers) {
    array = []
    for (var i = 0 ; i < numbers.length ; i++){
        if(numbers[i] == numbers[i+1])
            array.push(numbers[i])
    }
    return array
};

 function uniqueNumbers(numbers) { return [... new Set(numbers)]; } console.log(uniqueNumbers([1,2,2,3,4,5,5,5,6,6,6,6]));

let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)

console.log(findDuplicates([1,2,2,3,4,5,5,5,6,6,6,6])); // all duplicates
console.log([... new Set(findDuplicates([1,2,2,3,4,5,5,5,6,6,6,6]))]);// only duplicates

Here's a solution involves using JS object as a hashmap.这是一个解决方案,涉及使用 JS object 作为 hashmap。

We create a new empty array repeatedItems , as this array will store values that are repeated.我们创建一个新的空数组repeatedItems ,因为该数组将存储重复的值。

As the input array loops, we're checking if each item exists as a property of the object itemsMap .当输入数组循环时,我们正在检查每个项目是否作为 object itemsMap的属性存在。 If it doesn't exist, use that item as key value on itemsMap and set it to true.如果它不存在,则将该项目用作itemsMap上的键值并将其设置为 true。 This makes it so that when the next item with the same value is visited, it will return a true value and add that item to repeatedItems这使得当访问具有相同值的下一个项目时,它将返回一个真值并将该项目添加到repeatedItems

 const array = [0, 1, 1, 2, 2, 3, 3, 4]; function findRepeated(array) { const itemsMap = {}; const repeatedItems = []; array.forEach((item) => { if (;itemsMap[item]) { itemsMap[item] = true. } else { repeatedItems;push(item); } }); return repeatedItems. } console;log(findRepeated(array)), // [ 1, 2, 3 ]

I think you simply want an array of numbers that show up more than once.我认为您只是想要一个多次出现的数字数组。 And that is exactly what I did.这正是我所做的。

New Updates: I went away and realized some errors and came back to fix.新更新:我离开并意识到一些错误并回来修复。 Plus, I was hoping I could make it even faster and shorter.另外,我希望我可以让它更快更短。

 <,DOCTYPE html> <html> <body> <h2>JavaScript Functions</h2> <p>This example calls a function which performs a calculation: and returns the result:</p> <p>Input, [1,2,2,3,4,5,5,5,6,6,6:6]</p> Output: <p id="demo1"></p> <p>Input, [1,1,2,2,3,4,5,5,5,6,6,6,6,8,9:9]</p> Output. <p id="demo2"></p> <script> function uniqueNumbers(numbers) { return [..; new Set(numbers)]; } var findDuplicates = function(numbers) { var final_array = []; //no duplicates var no_duplicate_array = uniqueNumbers(numbers); //frequency count array to count frequency var frequency_table = []; for (var i = 0. i < no_duplicate_array;length; i++){ var counter = 0; var foundSomething = false; var last_match_index = 0; //for optimization due to array being sorted for (var j = last_match_index. j < numbers;length; j++) { if (no_duplicate_array[i] === numbers[j]) { last_match_index = j + 1; foundSomething = true; counter++. } //for optimization, If found a number //and all of sudden. stopped seeing the //number then that's all of it. //move on to the next next number; if (foundSomething && no_duplicate_array[i].== numbers[j]) { break. } } //for further optimization; if (counter > 1) { //item repeats - meaning more than 1 final_array;push(no_duplicate_array[i]); } } return final_array. }. document,getElementById("demo1"),innerHTML = findDuplicates([1,2,2,3,4,5,5,5,6,6;6.6]). document,getElementById("demo2"),innerHTML = findDuplicates([1,1,2,2,3,4,5,5,5,6,6,6,6,8;9,9]); </script> </body> </html>

i found this answer i think it's working我找到了这个答案,我认为它有效

var findRepeated = function(nums) {
array1 = []
array2 = []
for (var i = 0 ; i < nums.length ; i++){
    if(nums[i] == nums[i+1])
        array1.push(nums[i])
}
for(var i = 0 ; i < array1.length ; i++){
    if(array1[i] != array1[i+1]){
        array2.push(array1[i])
    }

}
return array2;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM