Remove duplicate elements from char array This is a method where a char[] will be supplied as an input and it should return an array, but without the duplicate elements. How can this be optimized?

private static char[] getMYCharArray(char[] array) {
        String myArray = "";
        for(int i = 0; i &lt; array.length; i++) {
            if(myArray.indexOf(array[i]) == -1) // check if a char already exist, if not exist then return -1
                myArray = myArray+array[i];      // add new char
        }
        return myArray.toCharArray();
    }
One possibility is to use a Set. Just add the elements from the array and then get the unique elements from the Set. This algorithm will be O(n) instead of O(n^2) because each element is accessed only once.

Note that you can use Arrays.asList() to simplify the coding, too.