print first non-repeating character

 


/**
 * Print1stNonRepeatingCH
 */
import java.util.*;

public class Print1stNonRepeatingCH {
    public static void Print(String str) {
        Queue<Character> q = new LinkedList<>();
        int freq[] = new int[26];
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            q.add(ch);
            freq[ch - 'a']++;
        }
        while (!q.isEmpty() && freq[q.peek() - 'a'] > 1) {
            q.remove();
        }
        if (q.isEmpty()) {
            System.out.println(-1);

        } else {
            System.out.print(q.peek());
        }
        System.out.println();
    }

    public static void main(String[] args) {
        String str = "aabccxb";
        Print(str);
    }
}

Comments