<문제 제시>
<문제설명>
문자열 my_string이 매개변수로 주어집니다.
my_string에서 중복된 문자를 제거하고 하나의 문자만 남긴 문자열을
return하도록 solution 함수를 완성해주세요.

<예시 입출력>

<문제 해결 과정>
문자열에서 중복된 문자를 제거하는 문제이다.
Try 1)
String answer = "";
HashSet<Character> temp = new HashSet<>();
for(int i = 0; i < my_string.length(); i++){
temp.add(my_string.charAt(i));
System.out.println(temp);
}
System.out.println(temp);
return answer;
자바의 자료구조 중 Set(세트)가 중복을 허용하지 않는 경우가 있어 HashSet을 사용해보았다.
HashSet을 사용한 결과, 순서에 상관없이 중복이 제거되어 저장되므로
p e o l 이 아닌 p e l o 가 저장된 것을 확인할 수 있었다.
Solution 1)
stream()의 distinct()메서드를 이용해 중복을 제거해보았다.
result = temp.stream().distinct().collect(Collectors.toList());
.collect(Collectors.toList()
>> 이 것을 collect 메서드를 활용하여 리스트로(toList) 변경시킨다.
<전체코드>
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class remove_duplication {
public static String solution(String my_string) {
String answer = "";
List<Character> temp = new ArrayList<>();
List<Character> result = new ArrayList<>();
for(int i = 0; i < my_string.length(); i++){
temp.add(my_string.charAt(i));
}
result = temp.stream().distinct().collect(Collectors.toList());
for(int i = 0; i < result.size(); i++) {
answer += result.get(i);
}
return answer;
}
public static void main(String[] args) {
String my_string = "people";
System.out.println(solution(my_string));
}
}
배운점으로 스트림에서 Collectors가 쓰인다는 것이었다. 또한 distinct()의 범용성에 대해서도 알 수 있었다.
Collectors : Stream을 일반적인 List, Set 등의 형태로 변경시키는 Stream 메서드
문제링크)
https://school.programmers.co.kr/learn/courses/30/lessons/120888
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'Algorithms > 프로그래머스' 카테고리의 다른 글
[프로그래머스] '캐릭터의 좌표' - Java (0) | 2023.05.12 |
---|---|
[프로그래머스] '가까운 수' - Java (0) | 2023.05.11 |
[프로그래머스] '소인수분해' - Java (0) | 2023.05.10 |
[프로그래머스] '숨어있는 숫자의 덧셈 (1)' - Java (0) | 2023.05.10 |
[프로그래머스] '문자열 정렬하기 (1)' - Java (0) | 2023.05.09 |
<문제 제시>
<문제설명>
문자열 my_string이 매개변수로 주어집니다.
my_string에서 중복된 문자를 제거하고 하나의 문자만 남긴 문자열을
return하도록 solution 함수를 완성해주세요.

<예시 입출력>

<문제 해결 과정>
문자열에서 중복된 문자를 제거하는 문제이다.
Try 1)
String answer = "";
HashSet<Character> temp = new HashSet<>();
for(int i = 0; i < my_string.length(); i++){
temp.add(my_string.charAt(i));
System.out.println(temp);
}
System.out.println(temp);
return answer;
자바의 자료구조 중 Set(세트)가 중복을 허용하지 않는 경우가 있어 HashSet을 사용해보았다.
HashSet을 사용한 결과, 순서에 상관없이 중복이 제거되어 저장되므로
p e o l 이 아닌 p e l o 가 저장된 것을 확인할 수 있었다.
Solution 1)
stream()의 distinct()메서드를 이용해 중복을 제거해보았다.
result = temp.stream().distinct().collect(Collectors.toList());
.collect(Collectors.toList()
>> 이 것을 collect 메서드를 활용하여 리스트로(toList) 변경시킨다.
<전체코드>
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class remove_duplication {
public static String solution(String my_string) {
String answer = "";
List<Character> temp = new ArrayList<>();
List<Character> result = new ArrayList<>();
for(int i = 0; i < my_string.length(); i++){
temp.add(my_string.charAt(i));
}
result = temp.stream().distinct().collect(Collectors.toList());
for(int i = 0; i < result.size(); i++) {
answer += result.get(i);
}
return answer;
}
public static void main(String[] args) {
String my_string = "people";
System.out.println(solution(my_string));
}
}
배운점으로 스트림에서 Collectors가 쓰인다는 것이었다. 또한 distinct()의 범용성에 대해서도 알 수 있었다.
Collectors : Stream을 일반적인 List, Set 등의 형태로 변경시키는 Stream 메서드
문제링크)
https://school.programmers.co.kr/learn/courses/30/lessons/120888
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'Algorithms > 프로그래머스' 카테고리의 다른 글
[프로그래머스] '캐릭터의 좌표' - Java (0) | 2023.05.12 |
---|---|
[프로그래머스] '가까운 수' - Java (0) | 2023.05.11 |
[프로그래머스] '소인수분해' - Java (0) | 2023.05.10 |
[프로그래머스] '숨어있는 숫자의 덧셈 (1)' - Java (0) | 2023.05.10 |
[프로그래머스] '문자열 정렬하기 (1)' - Java (0) | 2023.05.09 |