Adding an example code (the thing you’d already tried) is not only polite on StackOverflow, but also helps answering question about your problem – it’s easier to help with an example.
But, I’ll give it a try now:
const people = ['Mary', 'Paul', 'John', 'Lisa', 'Mary', 'Mary', 'Paul', 'Lisa'];
// counting the names & storing in an object
const countAppearance = ({
arr
}) => {
return arr.reduce((a, c) => {
if (typeof a[c] === "undefined") a[c] = 0
a[c]++
return a
}, {})
}
const ca = countAppearance({
arr: people
})
// getting names as an array by count number
const getAppearanceByCount = ({
obj,
count
}) => {
return Object.entries(obj).filter(([key, val]) => val <= count).reduce((a, c) => [...a, c[0]], [])
}
// getting names that appear twice
const countTwo = getAppearanceByCount({
obj: ca,
count: 2
})
console.log("two:", countTwo)
// getting names that appear three times
const countThree = getAppearanceByCount({
obj: ca,
count: 1
})
console.log("three:", countThree)
0
solved array return element based on number occurrence [closed]