使用ES6概念查找字符串数组中的重复元素

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

Finding recurring elements in an array of strings using ES6 concepts

问题

以下是您要翻译的内容:

"I came across a basic question of array in javascript of list out the recurring elements or duplicate elements in an array of strings.

So, lets say we have an array, arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']

So now I tried to solve this by below approach:

  1. function duplicateValues (arr) {
  2. let l = arr.length;
  3. for (let i=0; i<l; i++){
  4. if(arr[i+1] == arr[i]) {
  5. duplicate.push(arr[i]);
  6. }
  7. }
  8. return duplicate;
  9. }
  10. duplicateValues(['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']
  11. );

Expected output:

  1. ['Ash', 'Bob', 'Dylan']

But I am getting an empty array in the console. What should be the best approach to solve this problem?"

英文:

I came across a basic question of array in javascript of list out the recurring elements or duplicate elements in an array of strings.

So, lets say we have an array, arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']

So now I tried to solve this by below approach:

  1. var duplicate = [];
  2. function duplicateValues (arr) {
  3. let l = arr.length;
  4. for (let i=0; i<l; i++){
  5. if(arr[i+1] == arr[i]) {
  6. duplicate.push(arr[i]);
  7. }
  8. }
  9. return duplicate;
  10. }
  11. duplicateValues(['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']
  12. );

Expected output:

  1. ['Ash', 'Bob', 'Dylan']

But I am getting an empty array in the console. What should be the best approach to solve this problem?

答案1

得分: 0

这应该很简单,看下面。

  1. const d = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John'];
  2. const dub = d.filter(x => d.filter(f => f == x).length > 1).reduce((a, v) => {
  3. if (!a.find(x => x == v))
  4. a.push(v)
  5. return a
  6. }, []);
  7. console.log(dub)
英文:

This should be very simple, look below.

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

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

  1. const d = [&#39;Ash&#39;, &#39;Bob&#39;, &#39;Jarvis&#39;, &#39;Ash&#39;, &#39;Ash&#39;, &#39;Dylan&#39;, &#39;Bob&#39;, &#39;Dylan&#39;,&#39;Tom&#39;, &#39;John&#39;];
  2. const dub = d.filter(x=&gt; d.filter(f=&gt; f ==x).length&gt;1).reduce((a,v)=&gt; {
  3. if(!a.find(x=&gt; x == v))
  4. a.push(v)
  5. return a
  6. },[])
  7. console.log(dub)

<!-- end snippet -->

答案2

得分: 0

Wouldn't that be easier?

英文:

Wouldn't that be easier?

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

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

  1. const array = [&#39;Ash&#39;, &#39;Bob&#39;, &#39;Jarvis&#39;, &#39;Ash&#39;, &#39;Ash&#39;, &#39;Dylan&#39;, &#39;Bob&#39;, &#39;Dylan&#39;, &#39;Tom&#39;, &#39;John&#39;]
  2. const result = [...new Set(array.filter((e, i, a) =&gt; a.indexOf(e, i + 1) !== -1))]
  3. console.log(result)

<!-- end snippet -->

答案3

得分: 0

All you need to do is to just check if a map has that key.

  1. const arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan', 'Tom', 'John']
  2. let map = new Map();
  3. const dups = arr.reduce((acc, curr) => {
  4. if (map.has(curr.toLowerCase()) && acc.indexOf(curr) == -1) {
  5. acc.push(curr)
  6. } else {
  7. map.set(curr.toLowerCase(), curr)
  8. }
  9. return acc;
  10. }, []);
  11. console.log(dups)
英文:

All you need to do is to just check if a map has that key.

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

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

  1. const arr = [&#39;Ash&#39;, &#39;Bob&#39;, &#39;Jarvis&#39;, &#39;Ash&#39;, &#39;Ash&#39;, &#39;Dylan&#39;, &#39;Bob&#39;, &#39;Dylan&#39;, &#39;Tom&#39;, &#39;John&#39;]
  2. let map = new Map();
  3. const dups = arr.reduce((acc, curr) =&gt; {
  4. if (map.has(curr.toLowerCase()) &amp;&amp; acc.indexOf(curr) == -1) {
  5. acc.push(curr)
  6. } else {
  7. map.set(curr.toLowerCase(), curr)
  8. }
  9. return acc;
  10. }, []);
  11. console.log(dups)

<!-- end snippet -->

答案4

得分: 0

你可以使用 reduce 来获取数组中每个名字的频率,然后筛选只出现频率为2或更多次的条目。

这种方法避免了使用 findindexOf,这些方法会不必要地多次扫描数组。

  1. const arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan', 'Tom', 'John']
  2. console.log(Object.entries(arr.reduce((a,c)=> (a[c]??=0,a[c]++,a),{})).filter(([k,v])=>v>1).map(([k])=>k))

如果你有其他问题,请随时提出。

英文:

You could use reduce to get the frequency of each name in the array, then filter only those entries that appear with a frequency of 2 or more.

This approach avoids the use of find and indexOf, which would scan over the array an unnecessary number of times.

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

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

  1. const arr = [&#39;Ash&#39;, &#39;Bob&#39;, &#39;Jarvis&#39;, &#39;Ash&#39;, &#39;Ash&#39;, &#39;Dylan&#39;, &#39;Bob&#39;, &#39;Dylan&#39;,&#39;Tom&#39;, &#39;John&#39;]
  2. console.log(Object.entries(arr.reduce((a,c)=&gt;
  3. (a[c]??=0,a[c]++,a),{})).filter(([k,v])=&gt;v&gt;1).map(([k])=&gt;k))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月21日 00:55:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793165.html
匿名

发表评论

匿名网友

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

确定