@Test public void shouldFindNthMostRepeatedElement() { int[] numbers = { 1, 1, 2, 3, 4, 5, 2, 2, 2, 4, 4, 6, 7, 4, 9, 214, 4, 5, }; int result = findNthMostRepeatedElement.find(numbers, 2); assertEquals(1, result); }
public int find(int[] numbers, int position) { validateInput(numbers, position); Integer result = null; Map<Integer, Integer> counter = new HashMap<Integer, Integer>(); for (int i : numbers) { if (counter.get(i) == null) { counter.put(i, 1); } else { counter.put(i, counter.get(i) + 1); } } for (Integer candidate : counter.keySet()) { if (counter.get(candidate) == position) { result = candidate; break; } } validateResult(result); return result; }
FindNthMostRepeatedElement { public int find(int[] numbers, int position) { validateInput(numbers, position); Integer result = null; Map<Integer, Integer> counter = new HashMap<Integer, Integer>(); for (int i : numbers) { if (counter.get(i) == null) { counter.put(i, 1); } else { counter.put(i, counter.get(i) + 1); } } for (Integer candidate : counter.keySet()) { if (counter.get(candidate) == position) { result = candidate; break; } } validateResult(result); return result; } }
FindNthMostRepeatedElement { public int find(int[] numbers, int position) { validateInput(numbers, position); Integer result = null; Map<Integer, Integer> counter = new HashMap<Integer, Integer>(); for (int i : numbers) { if (counter.get(i) == null) { counter.put(i, 1); } else { counter.put(i, counter.get(i) + 1); } } for (Integer candidate : counter.keySet()) { if (counter.get(candidate) == position) { result = candidate; break; } } validateResult(result); return result; }  }
FindNthMostRepeatedElement { public int find(int[] numbers, int position) { validateInput(numbers, position); Integer result = null; Map<Integer, Integer> counter = new HashMap<Integer, Integer>(); for (int i : numbers) { if (counter.get(i) == null) { counter.put(i, 1); } else { counter.put(i, counter.get(i) + 1); } } for (Integer candidate : counter.keySet()) { if (counter.get(candidate) == position) { result = candidate; break; } } validateResult(result); return result; }  int find(int[] numbers, int position); }
FindNthMostRepeatedElement { public int find(int[] numbers, int position) { validateInput(numbers, position); Integer result = null; Map<Integer, Integer> counter = new HashMap<Integer, Integer>(); for (int i : numbers) { if (counter.get(i) == null) { counter.put(i, 1); } else { counter.put(i, counter.get(i) + 1); } } for (Integer candidate : counter.keySet()) { if (counter.get(candidate) == position) { result = candidate; break; } } validateResult(result); return result; }  int find(int[] numbers, int position);  }