Skip to content

Instantly share code, notes, and snippets.

@ravikp7
Last active May 20, 2019 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravikp7/0c1fc99297ef3e77a2589f15cfdddcc1 to your computer and use it in GitHub Desktop.
Save ravikp7/0c1fc99297ef3e77a2589f15cfdddcc1 to your computer and use it in GitHub Desktop.
function idBestUsers(...months) {
// Get user Ids present in all months
const firstMonth = months[0];
const filteredUsers = firstMonth.filter(userId => {
return months.every(month => month.includes(userId));
});
const userCount = {}; // Stores userId instance counts as key value pair
// Loop through all months and save userId count instances
for (let i = 0; i < months.length; i++) {
for (let j = 0; j < months[i].length; j++) {
const user = months[i][j];
if (filteredUsers.includes(user)) {
userCount[user] = (userCount[user] || 0) + 1;
}
}
}
const values = []; // Stores all counts for best users
for (let user in userCount) {
values.push(userCount[user]);
}
// Get unique userId count instances
const uniqueCounts = [...new Set(values)].sort((a, b) => b - a);
// Generate the desired output with array of count and user Ids
const result = uniqueCounts.map(count => {
const userIds = []; // Stores user Ids for same count
for (let user in userCount) {
if (userCount[user] === count) {
userIds.push(user);
}
}
userIds.sort();
return [count, userIds];
})
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment