-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountingDuplicates.java
69 lines (51 loc) · 1.96 KB
/
CountingDuplicates.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
package challeges_from_codewars;
import java.util.Arrays;
/*
Count the number of Duplicates
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
*/
/******SOLVED */
/**
* Algorithm I used was Convert to lowercase ,Sort it then covert it to
* String[] to iterate and compare , also compare with a temp variable
* if is not equal to the temp but it equal to current index then count
* should be incremented
* then return count
*/
public class CountingDuplicates{
public static void main(String[] args) {
// TESTING
duplicateCount("abcde");
duplicateCount("aabbcde");
duplicateCount("aabBcde");
duplicateCount("indivisibility");
duplicateCount("Indivisibilities");
duplicateCount("aA11");
duplicateCount("abcdeaB");
}
public static int duplicateCount(String text) {
int count = 0;
System.out.println(text);
String[] newString = text.toLowerCase().split("");
Arrays.sort(newString);
String temp = "";
for (int i = 0; i < text.length()-1; i++) {
if((newString[i].equals(newString[i+1])&& (!(newString[i].equals(temp))))){
count ++;
temp = newString[i];
}
}
System.out.println(Arrays.toString(newString));
System.out.println(count);
return count;
}
// Write your code here
}