Last active
May 23, 2020 22:34
-
-
Save kenduigraha/320661f58118f5e229a56474b1909415 to your computer and use it in GitHub Desktop.
This file contains 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
let solution = (n, users) => { | |
let answer = []; | |
let totalUsers = users.length; | |
let countDuplicateStageUsers = users.reduce((prev, curr) => { | |
prev[curr] = (prev[curr] || 0) + 1; | |
return prev; | |
}, {}); | |
// looping stages | |
for (let i = 1; i <= n; i++) { | |
let currObjKey = Object.keys(countDuplicateStageUsers)[i - 1]; | |
let failureStageCount = countDuplicateStageUsers[currObjKey] || 0; | |
let failureCount = 0; | |
for (var key in countDuplicateStageUsers) { | |
if (countDuplicateStageUsers.hasOwnProperty(key)) { | |
if (key <= currObjKey) { | |
failureCount += countDuplicateStageUsers[key]; | |
} | |
} | |
} | |
let totalCurrFaileur = totalUsers - failureCount; | |
let restUsers = failureStageCount + totalCurrFaileur; | |
let totalFailure = i === Number(currObjKey) ? failureStageCount : 0; | |
let failureRate = i === restUsers ? 1 : totalFailure / restUsers; | |
answer.push({ | |
stage: i, | |
failureRate, | |
}); | |
} | |
let sortDescAnswer = answer.sort((a, b) => b.failureRate - a.failureRate); | |
return sortDescAnswer.map((data) => data.stage); | |
}; | |
let n = 5; | |
let users = [2, 1, 2, 6, 2, 4, 3, 3]; | |
let users1 = [4, 4, 4, 4]; | |
// [3,4,2,1,5] | |
console.log(solution(n, users)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment