JS:打印重复的数值

huangapple go评论81阅读模式
英文:

JS: Array print the values that repeat

问题

需要打印数组中的重复项。

例如,给定以下数组和索引,该函数将打印 6,23,33,100

var array = [3,6,67,6,23,11,100,8,93,0,17,24,7,1,33,45,28,33,23,12,99,100];

显然,我们需要使用两个 'while 循环' 来完成这个任务。

我正在寻找一个解决方案,但更重要的是解释。链接将非常受欢迎。

谢谢!

英文:

I need to print the duplicates from an array.

For example, given the following array and index, the function will print 6,23,33,100.

var array = [3,6,67,6,23,11,100,8,93,0,17,24,7,1,33,45,28,33,23,12,99,100];

Apparently we need to do it using two 'while loops'.

Im looking for a solution, but more importantly an explanation.
Links are very much appreciated.

Thanks!

答案1

得分: 2

最优雅和高效的解决方案是使用一个while循环,只对数组进行一次迭代,因此,我们有O(N)的复杂度。

为此,我们需要声明一个hash,用于保存每个数组项的出现次数。如果我们找到一个重复的项,就将其存储在duplicates数组中。

英文:

The most elegant and efficient solution is to use a while loop which iterates the array only once, so, we have O(N) complexity.

For this, we need to declare an hash which keeps the number of occurencies for each array's item. If we found a duplicate one then we store it in duplicates array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var arr = [3,6,67,6,23,11,100,8,93,0,17,24,7,1,33,45,28,33,23,12,99,100], i = 0, hash = {}, duplicates = [];
while(i &lt; arr.length){
  hash[arr[i]] = hash[arr[i]] ? hash[arr[i]] += 1 : 1;
  if (hash[arr[i]] === 2) 
    duplicates.push(arr[i])
  i++;
}

console.log(duplicates)

<!-- end snippet -->

答案2

得分: 1

你可以使用filter()indexOf()方法。

var array = [3, 6, 67, 6, 23, 11, 100, 8, 93, 0, 17, 24, 7, 1, 33, 45, 28, 33, 23, 12, 99, 100];
console.log(array.filter((a, b, c) => c.indexOf(a) !== b));

其中:

  • a -> 是传递给箭头函数的值。
  • b -> 是传递进来的索引。
  • c -> 是传递进来的整个数组。

这行代码是根据原始数组是否存在一个值(参数a),其索引与通过箭头函数传递进来的索引(参数b)不匹配来过滤数组。

英文:

You could use the filter() and indexOf() methods.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var array = [3,6,67,6,23,11,100,8,93,0,17,24,7,1,33,45,28,33,23,12,99,100];
console.log(array.filter((a,b,c) =&gt; c.indexOf(a) !== b));

<!-- end snippet -->

a -> is the value being passed in the arrow function.

b -> is the index being passed in.

c -> is the whole array being passed in.

This line is filtering the array based on, if the original array has a value (argument a) whose index does not match the given index passed in through the arrow function (argument b).

答案3

得分: 0

以下是翻译好的部分:

A good sample and explanation can be found here... W3Resource

此处可以找到一个良好的示例和解释... W3Resource

Futhermore to assist in understanding the two major components of the code the Object...
Working with objects - Javascipt

此外,为了帮助理解代码的两个主要组件,对象...
使用对象 - JavaScript

and Arrays...Javascipt Arrays

和数组...JavaScript 数组

英文:

A good sample and explanation can be found here... W3Resource

Futhermore to assist in understanding the two major components of the code the Object...
Working with objects - Javascipt

and Arrays...Javascipt Arrays

答案4

得分: 0

以下是您要翻译的部分:

对于最短的方法,您可以使用一个闭包

(s => )( )

与一个Set

(s => )(new Set)

以及一个检查。如果一个值已经被看到,那么取这个值

(s => v => s.has(v) )(new Set)

或者将该值添加到集合中并返回false,因为不在结果集中的值不应该被看到。

(s => v => !s.add(v))(new Set)

var array = [3, 6, 67, 6, 23, 11, 100, 8, 93, 0, 17, 24, 7, 1, 33, 45, 28, 33, 23, 12, 99, 100],
    duplicates = array.filter((s => v => s.has(v) || !s.add(v))(new Set));

console.log(duplicates);
英文:

For the shortest approach, you could take a closure

(s =&gt;                           )(       )

with a Set

(s =&gt;                           )(new Set)

and a check. If a value is already seen, then take this value

(s =&gt; v =&gt; s.has(v)             )(new Set)

or add the value to the set and return false, because a not seen value should not in the result set.

(s =&gt; v =&gt;             !s.add(v))(new Set)

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var array = [3, 6, 67, 6, 23, 11, 100, 8, 93, 0, 17, 24, 7, 1, 33, 45, 28, 33, 23, 12, 99, 100],
duplicates = array.filter((s => v => s.has(v) || !s.add(v))(new Set));

console.log(duplicates);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 20:19:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定