-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstNonRepeated.java
More file actions
44 lines (34 loc) · 901 Bytes
/
FirstNonRepeated.java
File metadata and controls
44 lines (34 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.sai;
import java.util.Hashtable;
import java.util.Scanner;
public class FirstNonRepeated {
public static void main(String args[]) {
System.out.println("Enter the string: ");
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
char c = firstNonRepeatedChar(s);
System.out.println("First non repeated character is :" + c);
scan.close();
}
private static char firstNonRepeatedChar(String str) {
Hashtable<Character, Integer> charhashtable = new Hashtable<Character, Integer>();
int i, len;
len = str.length();
Character c;
for (i = 0; i < len; i++) {
c = str.charAt(i);
if (charhashtable.containsKey(c)) {
charhashtable.put(c, charhashtable.get(c) + 1);
} else {
charhashtable.put(c, 1);
}
}
for (i = 0; i < len; i++) {
c = str.charAt(i);
if (charhashtable.get(c) == 1) {
return c;
}
}
return 0;
}
}