본문 바로가기
Language/Algorithm

Remove duplicate characters

by Zayne 2023. 8. 26.

Example

Input : ksekkset
Output : kset

 

My answer:

import java.util.Scanner;

class CharacterUniqueFinder {
	public static void main(String[] args) {
		CharacterUniqueFinder t = new CharacterUniqueFinder();
		Scanner kb = new Scanner(System.in);
		String s = kb.nextLine();
		System.out.println(t.solution(s));
	}

	String solution(String s) {
		String answer = "";
		char[] ca = s.toCharArray();
		
		for(char x : ca) {
			if(answer.indexOf(x) == -1) {
				answer += x;
			}
		}

		return answer;
	}
	
}

1. It iterates through each character of the input string.

2. Checks if the character is not already present in the 'answer' string.

3. If not, it appends the character to the 'answer' string.

4. Finally, it returns the modified 'answer' string with duplicate characters removed.

 

'Language > Algorithm' 카테고리의 다른 글

Palindrome Checker - 2  (0) 2023.08.30
Palindrome Checker  (0) 2023.08.28
Reverse Alphabetic Characters  (0) 2023.08.25
Reverse String  (0) 2023.08.24
Find the longest word  (0) 2023.08.23