JavaScript solution (functional)


  • 0
    D
    var frequencySort = function(str) {
      const tally = str
        .split('')
        .reduce((obj,key) => {
          obj[key] ? obj[key]++ : obj[key] = 1;
          return obj;
        }, {});
    
      return Object
        .keys(tally)
        .map(key => key.repeat(tally[key]))
        .sort((a,b) => b.length - a.length)
        .join('')
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.