Find the first non repeating character of string
This is common interview question in many companies where they want to screening people based on their coding skill. Below is sample program:
package coding.test; public class StringNonRepeatingChar { public static void main(String[] args) { System.out.println("First non repeated char: ->"+firstNonRepeatedCharacter("tessetk")); } public static String firstNonRepeatedCharacter(String string) { for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); if (string.indexOf(c) == i && string.indexOf(c, i + 1) == -1) { return String.valueOf(c); } } return "No non repeated char found"; } }
Output:
The is not O(n).