/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem45;

/**
 * Given an array full of integers, can you write a method to find the element repeated kTH times.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class FindNthMostRepeatedElement {
}

@@ -645,16 +645,65 @@
 lem45;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * G
@@ -885,11 +885,1300 @@
 lement %7B
+%0A%0A  /**%0A   * Iterative solution to solve this problem. This algorithm is based on a complexity order equals%0A   * to O(N) and space O(N) because you need to go through the full set numbers two times and you%0A   * need a map to store the number of repetitions for every element.%0A   */%0A  public int find(int%5B%5D numbers, int position) %7B%0A    validateInput(numbers, position);%0A    Integer result = null;%0A    Map%3CInteger, Integer%3E counter = new HashMap%3CInteger, Integer%3E();%0A    for (int i : numbers) %7B%0A      if (counter.get(i) == null) %7B%0A        counter.put(i, 1);%0A      %7D else %7B%0A        counter.put(i, counter.get(i) + 1);%0A      %7D%0A    %7D%0A    for (Integer candidate : counter.keySet()) %7B%0A      if (counter.get(candidate) == position) %7B%0A        result = candidate;%0A        break;%0A      %7D%0A    %7D%0A    validateResult(result);%0A    return result;%0A  %7D%0A%0A  private void validateInput(int%5B%5D numbers, int position) %7B%0A    if (numbers == null %7C%7C position %3C= 0) %7B%0A      throw new IllegalArgumentException(%0A          %22You can't pass null arrays or position values minor than 0 as parameter.%22);%0A    %7D%0A  %7D%0A%0A  private void validateResult(Integer result) %7B%0A    if (result == null) %7B%0A      throw new IllegalArgumentException(%0A          %22There are no elements repeated n times in the array passed as argument%22);%0A    %7D%0A  %7D
 %0A%7D%0A

Add iterative solution to problem 45