Advertisement
Guest User

test

a guest
Dec 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package Test;
  2.  
  3. import java.util.ArrayList;
  4.  
  5.  
  6. /**
  7.  * Count the number of Duplicates
  8.  * Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that
  9.  * occur more than once in    * the input string. The input string can be assumed to contain only alphabets
  10.  * (both uppercase and lowercase) and numeric digits.
  11.  * <p>
  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 (bandB)
  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.  */
  21. public class App
  22. {
  23.     public static void main(String[] args)
  24.     {
  25.         System.out.println("Output should be 0: " + duplicateCount("abcde"));
  26.         System.out.println("Output should be 2: " + duplicateCount("aabbcde"));
  27.         System.out.println("Output should be 2: " + duplicateCount("aabBcde"));
  28.         System.out.println("Output should be 1: " + duplicateCount("indivisibility"));
  29.         System.out.println("Output should be 2: " + duplicateCount("Indivisibilities"));
  30.         System.out.println("Output should be 2: " + duplicateCount("aA11"));
  31.         System.out.println("Output should be 2: " + duplicateCount("ABBA"));
  32.     }
  33.  
  34.     public static int duplicateCount(String text)
  35.     {
  36.         //Start to find duplicates, then count it. Maybe stream it
  37.         char current = text.charAt(0);
  38.         int times = 0;
  39.  
  40.         for (int i = 0; i < text.length(); i++)
  41.         {
  42.             int j = 0;
  43.             while (j < text.length())
  44.             {
  45.                 if (current == text.charAt(i) && j == 0)
  46.                 {
  47.                     j++;
  48.                 }
  49.                 else
  50.                 {
  51.                     if (current == text.charAt(i))
  52.                     {
  53.                         times++;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.        
  59.         return times;
  60.     }
  61.  
  62.     //Add a method to count the times
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement