Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Set;
- import java.util.stream.Collectors;
- //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
- public class CountingDuplicates {
- public static void main(String[] args) {
- String s = "aabbcde";
- String zero = "abcde";
- String i = "Indivisibilities";
- System.out.println(duplicateCount(zero));
- }
- String s = "aabbcde";
- // It really depends on your requirements. Those sets simply represent three different use cases: if your "iterating" code
- //
- // needs only keys, iterate keySet()
- //
- // needs access to key + value; use entrySet()
- //
- // needs the values, use values()
- public static int duplicateCount(String text) {
- // Write your code here
- HashMap<Character, Integer>map = new HashMap<>();
- int count = 0;
- char[]chr = text.toCharArray();
- for (int i = 0; i < chr.length; i++) {
- if (!map.containsKey(chr[i])){
- map.put(chr[i],1);
- }
- else {
- map.put(chr[i] ,map.get(chr[i])+1);
- }
- }
- int value = 0;
- int resetvalue = 0;
- int total = 0;
- for(Character w : map.keySet()){
- value = map.get(w);
- System.out.println("what is the value" + value);
- if((value == 1) && total == map.size()) {
- resetvalue = 0;
- }
- if (value > 0 && total != map.size()){
- resetvalue = value;
- System.out.println(value+"\t"+w);
- }
- total += value;
- //System.out.println("total" + total);
- }
- return resetvalue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment