Skip to content

Commit 8eda532

Browse files
authored
Create Extract Unique characters
1 parent 5887e42 commit 8eda532

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Given a string S, you need to remove all the duplicates. That means, the output string should contain each character only once. The respective order of characters should remain same, as in the input string.
3+
4+
Input format:
5+
The first and only line of input contains a string, that denotes the value of S.
6+
7+
Output format :
8+
The first and only line of output contains the updated string, as described in the task.
9+
10+
Constraints :
11+
0 <= Length of S <= 10^8
12+
Time Limit: 1 sec
13+
14+
Sample Input 1 :
15+
ababacd
16+
Sample Output 1 :
17+
abcd
18+
19+
Sample Input 2 :
20+
abcde
21+
Sample Output 2 :
22+
abcde
23+
*/
24+
import java.util.HashMap;
25+
26+
public class Solution {
27+
28+
public static String uniqueChar(String str){
29+
/* Your class should be named Solution
30+
* Don't write main().
31+
* Don't read input, it is passed as function argument.
32+
* Return output and don't print it.
33+
* Taking input and printing output is handled automatically.
34+
*/
35+
HashMap<Character,Integer> map = new HashMap<>();
36+
String newstr="";
37+
for (int i=0;i<str.length();i++)
38+
{
39+
char c = str.charAt(i);
40+
if (map.containsKey(c))
41+
{
42+
map.put(c, map.get(c)+1);
43+
}
44+
else
45+
{
46+
newstr=newstr+c;
47+
map.put(c,1);
48+
}
49+
}
50+
return newstr;
51+
}
52+
}

0 commit comments

Comments
 (0)