Guest User

Untitled

a guest
Jan 2nd, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Set;
  3. import java.util.stream.Collectors;
  4.  
  5. //Count the number of Duplicates
  6. //
  7.  
  8. //Write a function that will return the count of distinct case-insensitive alphabetic characters
  9. //and numeric digits that occur more than once in the input string.
  10. //The input string can be assumed to contain only alphabets (both uppercase and lowercase)
  11. //and numeric digits.
  12. //Example
  13. //"abcde" -> 0 # no characters repeats more than once
  14. //"aabbcde" -> 2 # 'a' and 'b'
  15. //"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
  16. //"indivisibility" -> 1 # 'i' occurs six times
  17. //"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
  18. //"aA11" -> 2 # 'a' and '1'
  19. //"ABBA" -> 2 # 'A' and 'B' each occur twice
  20. public class CountingDuplicates {
  21.     public static void main(String[] args) {
  22.         String s = "aabbcde";
  23.         String zero = "abcde";
  24.         String i = "Indivisibilities";
  25.         System.out.println(duplicateCount(zero));
  26.     }
  27.    
  28.     String s = "aabbcde";
  29.  
  30.    
  31. //  It really depends on your requirements. Those sets simply represent three different use cases: if your "iterating" code
  32. //
  33. //    needs only keys, iterate keySet()
  34. //
  35. //    needs access to key + value; use entrySet()
  36. //
  37. //    needs the values, use values()
  38.  
  39.     public static int duplicateCount(String text) {
  40.         // Write your code here
  41.         HashMap<Character, Integer>map = new HashMap<>();
  42.         int count = 0;     
  43.         char[]chr = text.toCharArray();
  44.        
  45.         for (int i = 0; i < chr.length; i++) {             
  46.                 if (!map.containsKey(chr[i])){
  47.                     map.put(chr[i],1);
  48.                 }
  49.                 else {
  50.                     map.put(chr[i] ,map.get(chr[i])+1);
  51.                 }
  52.             }      
  53.         int value = 0;
  54.         int resetvalue = 0;
  55.         int total = 0;
  56.         for(Character w : map.keySet()){       
  57.            
  58.             value = map.get(w);
  59.             System.out.println("what is the value" + value);   
  60.             if((value == 1) && total == map.size()) {
  61.                 resetvalue = 0;            
  62.             }
  63.             if (value > 0 && total != map.size()){
  64.                 resetvalue = value;
  65.                 System.out.println(value+"\t"+w);
  66.             }
  67.             total += value;
  68.             //System.out.println("total" + total);
  69.         }
  70.         return resetvalue;
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment