Sorting algorithm | Sololearn: Learn to code for FREE!


Sorting algorithm

I have two objects, Artists and Songs. Artists={ A0: [ "a0", "a1", ...], A1: [ "a21", "a69", ...], ... //255 artists } Songs={ s0:{ ... album: "a0", ... }, s1:{ ... album: "a182", ... }, ... //26,500 songs } I need to find ten artists with most songs. I used this naive approach: let artists=Object.keys(Artists) .map(m=>[ m, Artists[m].map(n=> Object.keys(Songs) .filter(f=>Songs[f].album==n).length) .reduce((mm,n)=>mm+n) ]) .sort((m,n)=>n[1]-m[1]) .slice(0,10); But it takes 70 seconds to execute. Could you come up with a much faster approach? There are 805 albums. Artists object is here Songs object is here

6/2/2022 12:34:22 AM

Fernando Moceces

5 Answers

New Answer


ODLNT thank you very much for the insight. I have included the Artists and Songs objects on my post. Could you please try executing it again with your machine? Much appreciated.


Fernando Moceces based on your post I wrote the code below. I ran the algorithm provided, on my laptop got an average time of 3.9 seconds. I also ran it on bluestack virtual android phone and got an average time of 5.7second. I tracked the time using window.performance(), not for precision but for general knowledge. Take a look and let me know if the objects are formatted correctly.


Fernando Moceces Great now were on the same page. When I run your algorithm on my laptop with the objects you provided I see an average time of 3.6 seconds. I was able to come up with something faster using same object and timer on my laptop I get an average time of 0.015 seconds. I included screenshots and algorithm below I'm very interested what results you get on your device.


ODLNT excuse me but apparently you have your code set private so I can only see snippet of it. Also your google drive links seem to be inaccessible to public as well, they require access request.


My bad, I updated my previous post with public links to the screen shots and the code is now public