-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueLettersSet.java
More file actions
56 lines (41 loc) · 1.28 KB
/
Copy pathUniqueLettersSet.java
File metadata and controls
56 lines (41 loc) · 1.28 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.HashSet;
import java.util.*;
public class UniqueLettersSet{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter string..");
String input = scan.nextLine();
scan.close();
if(isUnique(input)){
System.out.println("All xters are unique");
}else{
System.out.println("All xters are not unique");
}
if(isUniqueArrSort(input)){
System.out.println("All xters are unique (Array Sort)");
}else{
System.out.println("All xters are not unique (Array Sort)");
}
}
private static boolean isUniqueArrSort(String input){
char[] chArr = input.toCharArray();
Arrays.sort(chArr);
for(int i = 0; i< chArr.length-1;i++){
if(chArr[i] != chArr[i+1]){
continue;
}else{
return false;
}
}
return true;
}
private static boolean isUnique(String str){
Set<Character> set = new HashSet<>();
char[] chArr = str.toCharArray();
for(Character c : chArr){
if(!set.add(c))
return false;
}
return true;
}
}