From c2fdfba49e8d7ea3b25ddd7a7aca759dfd9e4597 Mon Sep 17 00:00:00 2001 From: Vishwajeet-29-pro Date: Fri, 6 Dec 2024 08:14:06 +0530 Subject: [PATCH] feat: Add method for find first non-repeated character from string --- .../FindFirstNonRepeatedCharacter.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 JavaDsaWithTest/src/main/java/org/practice/dsa/strings/FindFirstNonRepeatedCharacter.java 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; + } +}