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

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

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:


function duplicateValues (arr) {
   let l = arr.length;

   for (let i=0; i<l; i++){
      if(arr[i+1] == arr[i]) {
     duplicate.push(arr[i]);
   }
 }
   return duplicate;
}

duplicateValues(['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']
);

Expected output:

['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:

var duplicate = [];

function duplicateValues (arr) {
   let l = arr.length;
   
   for (let i=0; i<l; i++){
      if(arr[i+1] == arr[i]) {
     duplicate.push(arr[i]);
   }
 }
   return duplicate;
}

duplicateValues(['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']
);

Expected output:

['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

这应该很简单,看下面。

const d = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John'];

const dub = d.filter(x => d.filter(f => f == x).length > 1).reduce((a, v) => {
  if (!a.find(x => x == v))
    a.push(v)
  return a
}, []);
console.log(dub)
英文:

This should be very simple, look below.

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

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

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;];

const dub = d.filter(x=&gt; d.filter(f=&gt; f ==x).length&gt;1).reduce((a,v)=&gt; {
  if(!a.find(x=&gt; x == v))
      a.push(v)
    return a
},[])
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 -->

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;]

const result = [...new Set(array.filter((e, i, a) =&gt; a.indexOf(e, i + 1) !== -1))]

console.log(result)

<!-- end snippet -->

答案3

得分: 0

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

const arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan', 'Tom', 'John']

let map = new Map();

const dups = arr.reduce((acc, curr) => {
  if (map.has(curr.toLowerCase()) && acc.indexOf(curr) == -1) {
    acc.push(curr)
  } else {
    map.set(curr.toLowerCase(), curr)
  }

  return acc;
}, []);

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 -->

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;]

let map = new Map();

const dups = arr.reduce((acc, curr) =&gt; {
  if (map.has(curr.toLowerCase()) &amp;&amp; acc.indexOf(curr) == -1) {
    acc.push(curr)
  } else {
    map.set(curr.toLowerCase(), curr)
  }

  return acc;
}, []);

console.log(dups)

<!-- end snippet -->

答案4

得分: 0

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

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

const arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan', 'Tom', 'John']

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 -->

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;]

console.log(Object.entries(arr.reduce((a,c)=&gt;
  (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:

确定