characterFrequency
Written on August 27, 2019
문제
Write a function that takes as its input a string and returns an array of arrays as shown below sorted in descending order by frequency and then by ascending order by character.
풀이
var characterFrequency = function(string) {
let arr = string.split("").sort();
let result = [];
let temp = [arr[0], 1];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === arr[i + 1]) {
temp[1] += 1;
} else {
result.push(temp);
temp = [arr[i + 1], 1];
}
}
result.sort((a, b) => {
if (a[1] > b[1]) return -1;
if (a[1] < b[1]) return 1;
if (a[0] > b[0]) return 1;
if (a[0] < b[0]) return -1;
});
return result;
};
👩🏻💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻♀️
in the process of becoming the best version of myself