-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathareumH.js
More file actions
34 lines (27 loc) · 890 Bytes
/
areumH.js
File metadata and controls
34 lines (27 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function getGenresCount(genres, plays) {
const genresCount = {};
genres.forEach((el, idx) => {
genresCount[el] = (genresCount[el] || 0) + plays[idx][0];
});
return Object.entries(genresCount)
.sort((a, b) => b[1] - a[1])
.map((el) => el[0]);
}
function solution(genres, plays) {
let answer = [];
const indexPlays = plays.map((el, idx) => [el, idx]);
const sortGenres = getGenresCount(genres, indexPlays);
const genresIndex = sortGenres.map((el) => {
return genres.map((genre, idx) => (el === genre ? idx : -1)).filter((idx) => idx !== -1);
});
for (let i = 0; i < genresIndex.length; i++) {
const selected = genresIndex[i].map((idx) => indexPlays[idx]);
answer.push(
selected
.sort((a, b) => b[0] - a[0])
.slice(0, 2)
.map((el) => el[1])
);
}
return answer.flat();
}