Advertisement
ErolKZ

Untitled

Nov 5th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1.  
  2. function solve (arr) {
  3.  
  4. let words = arr.shift().split(' ');
  5.  
  6. let obj = {};
  7.  
  8. let arr2 = [];
  9.  
  10.  
  11. arr.forEach(el => words.includes(el) ? obj[el] = (obj[el] || 0) + 1 : false);
  12.  
  13. // console.log(obj);
  14.  
  15. let sortedArr = Object.entries(obj).sort((a, b) => b[1] === a[1] ? a[0].localeCompare(b[0]) : b[1] - a[1]);
  16.  
  17. // console.log(sortedArr);
  18.  
  19.  
  20. for (let [key, value] of sortedArr) {
  21.  
  22. console.log(`${key} - ${value}`);
  23.  
  24. }
  25.  
  26.  
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement