Algorithm/codewars

Counting Duplicates 답안

Bonita SY 2021. 3. 24. 19:29
728x90
반응형

문제)

www.codewars.com/kata/54bf1c2cd5b56cc47f0007a1/train/javascript

 

Codewars: Achieve mastery through challenge

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

내가 푼 답안)

function duplicateCount(text){
  var lowerText = text.toLowerCase();
  var countCharDict = {};
  
  for (var i=0; i<lowerText.length; i++) {
    var t = lowerText[i];
    if (countCharDict[t] !== undefined) {
      countCharDict[t] += 1;
    } else {
      countCharDict[t] = 1;
    }
  }
  
  var countCharList = [];
  for (let key in countCharDict) {
    countCharList.push({ character : key, count: countCharDict[key] });
  }
  
  if (countCharList.length === 0) {
    return 0;
  }
  
  countCharList.sort((a, b) => {
    return b.count - a.count;
  });
  
  var maxCnt = 0;
  for (let elem of countCharList) {
    if (elem.count >= 2) {
      maxCnt += 1;
    } else {
      break;
    }
  }
  return maxCnt;
}

 

테스트 통과)

 

마음에 드는 다른 사람 소스)

와 어떻게 이렇게 요약이 되지;;; 다시 공부해야할 듯

728x90
반응형

'Algorithm > codewars' 카테고리의 다른 글

Equal Sides Of An Array 답안  (0) 2021.03.24
Are they the "same"? 답안  (0) 2021.03.24
Valid Braces 답안  (0) 2021.03.24
Calculating with Functions 답안  (0) 2021.03.24
Is this a triangle? 답안  (0) 2021.03.23