Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Test;
- import java.util.ArrayList;
- /**
- * 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.
- * <p>
- * Example
- * "abcde" -> 0 # no characters repeats more than once
- * "aabbcde" -> 2 # 'a' and 'b'
- * "aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB)
- * "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 App
- {
- public static void main(String[] args)
- {
- System.out.println("Output should be 0: " + duplicateCount("abcde"));
- System.out.println("Output should be 2: " + duplicateCount("aabbcde"));
- System.out.println("Output should be 2: " + duplicateCount("aabBcde"));
- System.out.println("Output should be 1: " + duplicateCount("indivisibility"));
- System.out.println("Output should be 2: " + duplicateCount("Indivisibilities"));
- System.out.println("Output should be 2: " + duplicateCount("aA11"));
- System.out.println("Output should be 2: " + duplicateCount("ABBA"));
- }
- public static int duplicateCount(String text)
- {
- //Start to find duplicates, then count it. Maybe stream it
- char current = text.charAt(0);
- int times = 0;
- for (int i = 0; i < text.length(); i++)
- {
- int j = 0;
- while (j < text.length())
- {
- if (current == text.charAt(i) && j == 0)
- {
- j++;
- }
- else
- {
- if (current == text.charAt(i))
- {
- times++;
- }
- }
- }
- }
- return times;
- }
- //Add a method to count the times
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement