英文:
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 = ['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)
<!-- 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 = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan', 'Tom', 'John']
const result = [...new Set(array.filter((e, i, a) => 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 = ['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)
<!-- end snippet -->
答案4
得分: 0
你可以使用 reduce
来获取数组中每个名字的频率,然后筛选只出现频率为2或更多次的条目。
这种方法避免了使用 find
和 indexOf
,这些方法会不必要地多次扫描数组。
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 = ['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))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论