Last active
May 20, 2019 07:57
-
-
Save ravikp7/0c1fc99297ef3e77a2589f15cfdddcc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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