Skip to content

Commit

Permalink
feat: Add method for find first non-repeated character from string
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwajeet-29-pro committed Dec 6, 2024
1 parent d291beb commit c2fdfba
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit c2fdfba

Please sign in to comment.