Java Program to find unique characters in a string


In this program, You will learn how to find unique characters in a string in java.


Enter string: level

List of unique char is: v

Example: How to find unique characters in a string in java.

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        String str;
        int j, i = 0, count = 0, l = 0;

        System.out.print("Enter a string:");
        str = s.nextLine();

        char a[] = str.toCharArray();
        char b[] = new char[100];
        b[0] = '0';

        for (j = 0; j < a.length; j++) {
            i++;
        }

        for (j = 0; j < i;) {
            count = 0;

            for (int k = 0; k < i; k++) {
                if (a[j] == a[k]) {
                    count++;
                }
            }
            if (count == 1) {
                l++;
                b[l] = a[j];
                j++;

            } else {
                j++;
            }
        }

        System.out.print("List of unique char:");
        for (int m = 1; m <= l; m++) {
            System.out.print(b[m] + " ");
        }

    }
}

Output:

Enter a string:Xiith
List of unique char:X t h