Created
October 8, 2015 21:50
-
-
Save connordavison/611e1b036c7b540fbb9d to your computer and use it in GitHub Desktop.
Counter number of n-duplicates in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashMap; | |
| import java.util.Map; | |
| class Solution { | |
| public static int solve(int[] A, int n) { | |
| HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>(); | |
| int i; | |
| for (int a : A) { | |
| i = counts.containsKey(a) ? counts.get(a) + 1 : 1; | |
| counts.put(a, i); | |
| } | |
| int num_n_duplicates = 0; | |
| for (Map.Entry<Integer, Integer> c : counts.entrySet()) { | |
| if (n == c.getValue()) num_n_duplicates++; | |
| } | |
| return num_n_duplicates; | |
| } | |
| public static void main(String[] args) { | |
| int[] A = {0, 0, 1, 1, 1, 2, 2, 3, 3, 3}; | |
| System.out.println("Starting..."); | |
| // Should return 2 | |
| System.out.println(Solution.solve(A, 2)); | |
| // Should return 2 | |
| System.out.println(Solution.solve(A, 3)); | |
| // Should return 0 | |
| System.out.println(Solution.solve(A, 0)); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: