-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestAlbum.js
36 lines (30 loc) · 868 Bytes
/
BestAlbum.js
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
35
36
function solution(genres, plays) {
const array = [];
const length = genres.length;
for (let i = 0; i < length; i++) {
array.push({ genre: genres[i], play: plays[i], index: i });
}
const genrePlayCount = array.reduce((acc, cur) => {
if (!acc[cur.genre]) {
acc[cur.genre] = 0;
}
acc[cur.genre] += cur.play;
return acc;
}, {});
const sortedGenres = Object.keys(genrePlayCount).sort(
(a, b) => genrePlayCount[b] - genrePlayCount[a]
);
const sortedArray = [];
sortedGenres.forEach((genre) => {
const songs = array.filter((song) => song.genre === genre);
songs.sort((a, b) => b.play - a.play);
sortedArray.push(...songs.slice(0, 2));
});
return sortedArray.map((song) => song.index);
}
console.log(
solution(
["classic", "pop", "classic", "classic", "pop"],
[500, 600, 150, 800, 2500]
)
);