728x90

코딩테스트 연습 - 중복된 문자 제거 | 프로그래머스 스쿨

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

내가 생각했던 것 (오답)

char[] charArr = my_string.ToCharArray();
List<char> charList = charArr.ToList();

for (int i = 0; i < charList.Count; i++)
{
    int result = 0;
    for (int j = 0; j < charList.Count; j++)
    {
        if (charList[i] == charList[j])
        {
            result++;
            if (result >= 2)
            {
                int findex = charList.FindLastIndex(x => x == charList[j]);
                if (findex != -1)
                {
                    charList.RemoveAt(findex);
                }
            }
        }
    }
}
    return new string(charList.ToArray());

공백 제거가 안됐을 뿐더러 매우 비효율적인 구조

 

클린한 답

return new string(my_string.Distinct().ToArray());
728x90

'프로그래머스' 카테고리의 다른 글

암호 해독  (0) 2023.10.18
로그인 성공  (0) 2023.10.18
직각삼각형 출력  (0) 2023.10.18
제곱수 판별  (0) 2023.10.18
숨어있는 숫자의 덧셈  (0) 2023.10.17
Posted by 바르마스
,