Created
June 11, 2025 08:04
-
-
Save mateja176/b816bb56619a66ef41e742e3381ebe81 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
const listOfPasswords = [ | |
"password", | |
"blink182", | |
"12345678", | |
"whateverPass", | |
"password", | |
"iamyourfather", | |
"12345678", | |
"blink182", | |
"password", | |
"password", | |
"blink182", | |
"12345678", | |
]; | |
const findKTopPasswords = ( | |
listOfPasswords: string[], | |
topStringsNumber: number, | |
) => { | |
const counter = listOfPasswords.reduce<{ [K in string]: number }>((acc, current) => { | |
return { | |
...acc, | |
[current]: (acc[current] ?? 0) + 1 | |
} | |
}, {}) | |
const entries = Object.entries(counter) | |
// const occurences = entries.sort(([, a], [, b]) => b - a) | |
const occurences = entries.reduce<{ [N in string]: Array<[string, number]> }>((acc, [password, count]) => { | |
return { | |
...acc, | |
[count]: (acc[count] ?? []).concat([password, count]) | |
// [count]: [...(acc[count] ?? []), [password, count]] | |
} | |
}, {}) | |
// { | |
// 3: [['blink182', 3], ['123456768', 3]] | |
// } | |
// return occurences.slice(0, topStringsNumber) | |
console.log(occurences) | |
return Object.values(occurences) | |
.sort(([[, countA]], [[, countB]]) => countB - countA) | |
.slice(0, topStringsNumber) | |
.reduce((acc, current) => acc.concat(current), []) | |
}; | |
const top2Passwords = findKTopPasswords(listOfPasswords, 2); | |
console.log(top2Passwords); | |
// Should return | |
// [ | |
// [ "password", 4 ], | |
// [ "blink182", 3 ] | |
// ] | |
// | |
// Should return* | |
// [ | |
// [ "password", 4 ], | |
// [ "blink182", 3 ] | |
// [ "12345678", 3 ] | |
// ] | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment