/*
 * 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.app.problem10;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Given an array full of integers, can you write a method returning other array without duplicated
 * elements? The elements order doesn't care. For example:
 *
 * Input: [1,2,3,1] Output: [1,1,12]
 * Input: [1,1,1,1,1] Output: [1]
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class RemoveDuplicates {

  /**
   * Implementation based on Java Sets. To solve this algorithm we have used a Java HashSet and the
   * complexity order of this algorithm in time is O(N) where N is the number of elements in the
   * input array. In space terms, the complexity order of this algorithm is also O(N), because we
   * are using an auxiliary data structure.
   *
   * This implementation is possible in O(N) because the complexity order of HashSets for
   * insertions is O(1).
   */
  public Integer[] removeUsingSet(int[] numbers) {
    if (numbers == null) {
      throw new IllegalArgumentException("You can't pass a null array");
    }

    Set<Integer> uniqueElements = new HashSet<Integer>();
    for (int i : numbers) {
      uniqueElements.add(i);
    }
    return uniqueElements.toArray(new Integer[uniqueElements.size()]);
  }

  /**
   * Slow implementation for this algorithm. First of all, sort the array using QuickSort algorithm
   * and then, go through the array and add to a list just those consecutive elements that are
   * different.
   *
   * The complexity order of this algorithm in space terms is O(N) where N is the number of
   * elements in the input array. The complexity order in time terms is equals to the sorting
   * algorithm, O( N log(N)).
   */
  public Integer[] removeUsingSorting(Integer[] array) {
    if (array == null) {
      throw new IllegalArgumentException("You can't pass a null array");
    }
    if (array.length == 1) {
      return array;
    }

    Collections.sort(Arrays.asList(array));
    List<Integer> result = new LinkedList<Integer>();
    for (int i = 0; i < array.length - 1; i++) {
      if (!array[i].equals(array[i + 1])) {
        result.add(i);
      }
    }
    return result.toArray(new Integer[result.size()]);
  }
}

@@ -2440,21 +2440,23 @@
 teger%5B%5D 
-array
+numbers
 ) %7B%0A    
@@ -2459,21 +2459,23 @@
     if (
-array
+numbers
  == null
@@ -2565,21 +2565,23 @@
     if (
-array
+numbers
 .length 
@@ -2601,21 +2601,23 @@
  return 
-array
+numbers
 ;%0A    %7D%0A
@@ -2652,21 +2652,23 @@
 .asList(
-array
+numbers
 ));%0A    
@@ -2741,21 +2741,23 @@
  0; i %3C 
-array
+numbers
 .length 
@@ -2779,21 +2779,23 @@
    if (!
-array
+numbers
 %5Bi%5D.equa
@@ -2797,21 +2797,23 @@
 .equals(
-array
+numbers
 %5Bi + 1%5D)

Rename array parameter to numbers