diff --git a/JavaDsaWithTest/src/main/java/org/practice/dsa/strings/FindFirstNonRepeatedCharacter.java b/JavaDsaWithTest/src/main/java/org/practice/dsa/strings/FindFirstNonRepeatedCharacter.java new file mode 100644 index 0000000..5192f0e --- /dev/null +++ b/JavaDsaWithTest/src/main/java/org/practice/dsa/strings/FindFirstNonRepeatedCharacter.java @@ -0,0 +1,22 @@ +package org.practice.dsa.strings; + +public class FindFirstNonRepeatedCharacter { + public static void main(String[] args) { + System.out.println(findFirstNonRepeated("swiss")); + } + + public static Character findFirstNonRepeated(String str) { + int[] freq = new int[256]; + + for (char c: str.toCharArray()) { + freq[c]++; + } + + for (char c: str.toCharArray()) { + if (freq[c] == 1) { + return c; + } + } + return null; + } +}