-1

I have a given char array and have to remove the duplicate letters in it. How can I do this in Java?

Example:

Given char array:

char[] s = { 'H','e','l','l','o','W','o','r','l','d','!'};

Expected result:

char[] s = { 'H','e','l','o','W','r','d','!'};
Rene Knop
  • 1,788
  • 3
  • 15
  • 27

1 Answers1

1

You can remove duplicates like this and get a new char array,

public class Main {
    public static void main(String[] args) {
        char[] array = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd', '!'};
        String temp = "";
        for (int i = 0; i < array.length; i++) {
            if (temp.indexOf(array[i]) == -1)
                temp = temp + array[i];
        }
        char[] reslut = temp.toCharArray();
    }
}
Sandeepa
  • 3,457
  • 5
  • 25
  • 41