Javascript 数组 - 查找重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17968987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:15:45  来源:igfitidea点击:

Javascript Arrays - Find Duplicates

javascript

提问by Filth

Here is my question...

这是我的问题...

Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array. For example, given the array [ 1, 2, 4, 4, 3, 3, 1, 5, 3 ] it should return [1, 4, 3]. For extra bonus points return a sorted array.

给定一个用数字填充的数组作为函数参数,生成一个包含数组中任何重复数字的结果数组。例如,给定数组 [ 1, 2, 4, 4, 3, 3, 1, 5, 3 ] 它应该返回 [1, 4, 3]。对于额外的奖励积分,返回一个排序的数组。

I am starting out with Javascript - I know the language however, using it in the correct way ( as one should ) I'm still getting to grips with.

我是从 Javascript 开始的——但是我知道这门语言,以正确的方式使用它(正如人们应该的那样)我仍然在掌握它。

My pseudo code for this would be to:

我的伪代码是:

Create an array with the numbers above var numbers = [1, 2, 4, 4, 3, 3, 1, 5, 3];

用上面的数字创建一个数组 var numbers = [1, 2, 4, 4, 3, 3, 1, 5, 3];

Then create an empty array named "result" var result = [];

然后创建一个名为“result”的空数组 var result = [];

Create a for loop that goes through the var numbers to check for duplicates which will then populate the empty array "result" with the duplicates

创建一个遍历 var 编号的 for 循环以检查重复项,然后用重复项填充空数组“结果”

for (var i = 0;i < numbers.length; i++) {
 //This is where I'm stuck...
}

I'm not sure what to do within the for loop to populate the var result and to throw in to the mix... The given array has to be a function parameter which makes sense so you can change the numbers in one place.

我不确定在 for 循环中做什么来填充 var 结果并投入混合......给定的数组必须是一个有意义的函数参数,以便您可以在一个地方更改数字。

Any feedback on my thought process on this so far is greatly appreciated but ultimately I am wanting to learn how to achieve this.

到目前为止,对我在这方面的思考过程的任何反馈都非常感谢,但最终我想学习如何实现这一目标。

Here is a JSFiddle of my progress so far... http://jsfiddle.net/fbauW/

这是迄今为止我的进展的 JSFiddle ...... http://jsfiddle.net/fbauW/

回答by

One way of doing this (and it's not the only way) is by checking for existing elements in the array. Take a look at JavaScript's lastIndexOf function:

这样做的一种方法(它不是唯一的方法)是检查数组中的现有元素。看看 JavaScript 的 lastIndexOf 函数:

http://www.w3schools.com/jsref/jsref_lastindexof_array.asp

http://www.w3schools.com/jsref/jsref_lastindexof_array.asp

It will return -1 if the object does not exist in your array, and if it exists, will return an index of a later position than you are in. So you can use an if statement in your loop that checks whether or not there is another index containing your number, and add it in to your results array IF AND ONLY IF the index you get back != the index you are currently on (if they equal, this means that there is only one of that element in the list).

如果该对象不存在于您的数组中,它将返回 -1,如果它存在,将返回一个比您所在位置晚的索引。因此,您可以在循环中使用 if 语句来检查是否存在包含您的数字的另一个索引,并将其添加到您的结果数组中,如果且仅当您返回索引时!=您当前所在的索引(如果它们相等,这意味着列表中只有该元素之一) .

If you need more help, comment here and I can type some code in!

如果您需要更多帮助,请在此处发表评论,我可以输入一些代码!

Good luck!

祝你好运!

回答by Lonely

Array.prototype.contains = function(k) {
  for ( var p in this)
    if (this[p] === k)
      return true;
  return false;
};
//this prototype function checks if an element is already in the array or not
//go through all the array and push the element to result if it is not 
//this way we can eliminate duplicates 
//result will contain the resultant array
function findDuplicates(Numbers) {
  var arrayLength = Numbers.length, i, j, result = [];
  for (i = 0; i < arrayLength; i++) {
    for (j = 0; j < arrayLength; j++) {
      if (a[i] == a[j] && i != j && !result.contains(a[i])) {
        result.push(a[i]);
      }
    }
  }
  return result;
}