String_중복문자 제거_arraylist, charAt, indexOf
✅ 문자열에서 중복된 문자를 제거하고 출력
단, 중복이 제거된 문자열의 각 문자는 원래 문자열의 순서를 유지한다.
🟢 ArrayList
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Main {
public String solution(String str){
String answer="";
char[] charArr= str.toCharArray();
ArrayList<Character> answerList= new ArrayList<>();
for(char c: charArr){
if(!answerList.contains(c)){
answerList.add(c);
answer +=c;
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner sc= new Scanner(System.in);
String str= sc.next();
System.out.println(T.solution(str));
}
}
//⭐️input:
//ksekkset
//⭐️output:
//kset
🟢 chatAt, indexOf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Main {
public String solution(String str){
String answer="";
for(int i=0; i<str.length(); i++){
System.out.println(str.charAt(i)+ " " + i + " " + str.indexOf(str.charAt(i)));
if(i == str.indexOf(str.charAt(i))){
answer +=str.charAt(i);
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner sc= new Scanner(System.in);
String str= sc.next();
System.out.println(T.solution(str));
}
}
🔵 ThingsILearned
✔️ indexOf()
는 그 문자가 가장 먼저 발견된 index를 return
This post is licensed under CC BY 4.0 by the author.