728x90
https://www.codewars.com/kata/54bf1c2cd5b56cc47f0007a1/train/javascript
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
오랜만에 코드워를 했다.
랭크업을 눌러봤는데, 이번엔 6 Kyu 짜리를 하게 되었다.
일단 들어오는 text string에 총 몇개의 중복이 있는지 찾는거다.
나는 보는데 stack이 떠올랐다. 그래서 그냥 넣고 빼기를 하려다가,
그냥 빈 배열을 하나 만들고, Set을 하나 만든다음에 둘을 잘 분배하는쪽으로 진행했다.
// Count the number of Duplicates
// Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
// Example
// "abcde" -> 0 # no characters repeats more than once
// "aabbcde" -> 2 # 'a' and 'b'
// "aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
// "indivisibility" -> 1 # 'i' occurs six times
// "Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
// "aA11" -> 2 # 'a' and '1'
// "ABBA" -> 2 # 'A' and 'B' each occur twice
function duplicateCount(text) {
if (text === "") return 0;
const arrA = [];
const arrB = new Set();
text
.toLowerCase()
.split("")
.forEach((letter) => {
if (!arrA.includes(letter)) {
arrA.push(letter);
} else {
arrB.add(letter);
}
});
return arrB.size;
}
console.log(duplicateCount("abcde"));
console.log(duplicateCount("aabbcde"));
console.log(duplicateCount("aabBcde"));
console.log(duplicateCount("indivisibility"));
console.log(duplicateCount("Indivisibilities"));
console.log(duplicateCount("aA11"));
console.log(duplicateCount("ABBA"));
728x90
'알고리즘' 카테고리의 다른 글
[CodeWars] 6Kyu : Take a Ten Minutes Walk (2) | 2025.08.06 |
---|---|
[CodeWars] 5Kyu : Memoized Fibonacci (1) | 2024.12.10 |
[CodeWars] 6Kyu : Unique In Order (0) | 2024.12.08 |
[CodeWars] 7Kyu : Sum a list but ignore any duplicates (1) | 2024.12.08 |
[CodeWars] 7Kyu : Descending Order (2) | 2024.12.06 |