Java Array Duplicate removeDuplicates(int[] input)

Here you can find the source of removeDuplicates(int[] input)

Description

Remove duplicate elements from an int array

License

Open Source License

Parameter

Parameter Description
input input unsorted int array

Return

a sorted int array with unique elements

Declaration

public static int[] removeDuplicates(int[] input) 

Method Source Code

//package com.java2s;
/**//from   w w  w.  j a v a 2  s  .com
 * Copyright (c) 2014-2016 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 * 
 * Change History - most recent changes go on top of previous changes
 *
 * ArrayUtils.java
 *
 * Who   Date       Description
 * ====  =========  ======================================================================
 * WY    14Jun2015  Bug fix for toNBits() to use long data type internally
 * WY    04Jun2015  Rewrote all concatenation related methods
 * WY    02Jun2015  Bug fix for generic concatenate methods
 * WY    06Apr2015  Added reverse(byte[]) to reverse byte array elements
 * WY    06Jan2015  Added reverse() to reverse array elements
 * WY    10Dec2014  Moved reverseBits() from IMGUtils to here along with BIT_REVERSE_TABLE
 * WY    08Dec2014  Fixed bug for flipEndian() with more than 32 bit sample data 
 * WY    07Dec2014  Changed method names for byte array to other array types conversion
 * WY    07Dec2014  Added new methods to work with floating point TIFF images
 * WY    03Dec2014  Added byteArrayToFloatArray() and byteArrayToDoubleArray()
 * WY    25Nov2014  Added removeDuplicates() to sort and remove duplicates from int arrays
 * WY    12Nov2014  Changed the argument sequence for flipEndian()
 * WY    11Nov2014  Changed flipEndian() to include scan line stride to skip bits
 * WY    11Nov2014  Added toNBits() to convert byte array to nBits data unit
 * WY    28Oct2014  Added flipEndian() to work with TIFTweaker mergeTiffImagesEx()
 */

import java.util.Arrays;

public class Main {
    /**
     * Remove duplicate elements from an int array
     * 
     * @param input input unsorted int array
     * @return a sorted int array with unique elements
     */
    public static int[] removeDuplicates(int[] input) {
        //return if the array length is less than 2
        if (input.length < 2) {
            return input;
        }

        // Sort the array first
        Arrays.sort(input);

        int j = 0;
        int i = 1;

        while (i < input.length) {
            if (input[i] == input[j]) {
                i++;
            } else {
                input[++j] = input[i++];
            }
        }

        int[] output = new int[j + 1];

        System.arraycopy(input, 0, output, 0, j + 1);

        return output;
    }
}

Related

  1. eraseDuplicatedValue(String[] srcArr)
  2. hasDuplicates(final T[] array)
  3. isDuplicated(String[] strArray)
  4. removeDuplicates(double[] array)
  5. removeDuplicates(final Object[] array)
  6. removeDuplicates(T[] elements)
  7. removeDuplicatesInPath(int[][] path)
  8. removeDuplicateStrings(String[] array)
  9. removeDuplicateValues(double[] values)