메모리: 64.7 MB, 시간: 1.92 ms
코딩테스트 연습 > 코딩테스트 입문
정확성: 100.0
합계: 100.0 / 100.0
문자열 my_string이 매개변수로 주어집니다. my_string에서 중복된 문자를 제거하고 하나의 문자만 남긴 문자열을 return하도록 solution 함수를 완성해주세요.
my_string ≤ 110my_string은 대문자, 소문자, 공백으로 구성되어 있습니다.| my_string | result |
|---|---|
| "people" | "peol" |
| "We are the world" | "We arthwold" |
입출력 예 #1
입출력 예 #2
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
class Solution {
public String solution(String my_string) {
String[] split = my_string.split("");
String answer = Arrays.asList(split).stream().distinct().collect(Collectors.joining());
return answer;
}
}
처음 짠 코드에서는 매겨변수로 넘어온 my_String을 split하여 Stirng[]에 저장후
다시 List로 변환후
list로 Stream을 열어 중복제거, collect로 이어붙여줬는데
굳이그럴 필요가없었다
String[] 은 전혀 필요하지도 않았고
배열이 필요하지않으니 asList를 쓸필요도 없었다.