prototip,
let input = "aaa bb bb rrrrr aaa r t";
function countWords(str, number){
str = str.split(/\s+/);
const obj = str.reduce((a, b) => (a[b] = (a[b]||0) + 1, a), {});
str = Object.keys(obj).sort((a, b) => (obj[b] - obj[a]) || (b.length - a.length));
return str.slice(0, number);
}
alert(countWords(input, 2));