-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add method for find first non-repeated character from string
- Loading branch information
1 parent
d291beb
commit c2fdfba
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
JavaDsaWithTest/src/main/java/org/practice/dsa/strings/FindFirstNonRepeatedCharacter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |