//  Copyright 2016 The Sawdust Open Source Project
//
// 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 array;

import java.util.HashSet;
import java.util.Set;

/**
 * Let A be an array of size n ≥ 2 containing integers from 1 to n−1 inclusive, one
 * of which is repeated. Describe an algorithm for finding the integer in A that is
 * repeated.
 */
public class FindRepeatedInteger {
    public static int getTheRepeatedInteger(int[] array) {
        Set set = new HashSet(array.length - 1);
        for (int i = 0; i < array.length; i++) {
            if (set.contains(array[i])) {
                return array[i];
            }
            set.add(array[i]);
        }
        return 0;
    }

    public static int getTheRepeatedInteger2(int[] array) {
        int sum = array[0];
        int s = 0;
        for (int i = 1; i < array.length; i++) {
            sum += array[i];
            s += i;
        }
        return sum - s;
    }
}

@@ -675,16 +675,25 @@
 t;%0A%0A/**%0A
+ * %3Cpre%3E%0A
  * Let A
@@ -767,15 +767,96 @@
 ive,
+%0A *%0A *  e.g. n = 5%0A *  3 1 2 3 4%0A *  0 1 2 3 4%0A *%0A *  4 1 2 3 4%0A *  0 1 2 3 4%0A *%0A * 
  one
-%0A *
  of 
@@ -935,16 +935,17 @@
 at is%0A *
+ 
  repeate

Add comments