Find Unique Characters in the String

Solution Steps:

  1. Obtain char array from the input String
  2. Loop through the char array and add each char to a Set<Character>
package com.java.programs;
import java.util.Set;
import java.util.TreeSet;
public class UniqueCharacters {
	
	public static Set<Character> uniqueCharacters(String input){
		Set<Character> characters = new TreeSet<>();
		for(char c: input.toCharArray()) {
			characters.add(c);
		}
		return characters;
	}
	
	public static void main(String[] args) {
		System.out.println(uniqueCharacters("aeroplane"));
	}
}
 [a, e, l, n, o, p, r]

Leave a Reply

Your email address will not be published. Required fields are marked *