Java Array Unique unique(int[] array)

Here you can find the source of unique(int[] array)

Description

Returns the same values of the input array but with no repetitions.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

Same values as in array but with no repetitions

Declaration

public static int[] unique(int[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*from ww w. j  av  a 2s. c  om*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Returns the same values of the input {@code array} but with no repetitions. There is no order guarantee.
     *
     * @param array Input array
     * @return Same values as in {@code array} but with no repetitions
     *  
     */
    public static int[] unique(int[] array) {
        Set<Integer> uniqueSet = new LinkedHashSet<Integer>();
        for (int i = 0; i < array.length; i++)
            uniqueSet.add(array[i]);

        return toArray(uniqueSet);
    }

    /**
     * Converts an array of {@code String} with the values, into a {@code int} array. Uses {@code Integer.parseInt} to parse the number, an can raise the exceptions 
     * that this method raises
     *
     * @param list Input list
     * @return output array
     */
    public static int[] toArray(String[] list) {
        int[] res = new int[list.length];
        int counter = 0;
        for (String s : list)
            res[counter++] = Integer.parseInt(s);
        return res;
    }

    /**
     * Converts a collection ({@code List}, {@code Set}...) of {@code Integer} objects to an {@code int} array.
     *
     * @param list Input list
     * @return {@code int} array
     *  
     */
    public static int[] toArray(Collection<Integer> list) {
        return asPrimitiveArray(list.toArray(new Integer[list.size()]));
    }

    /**
     * Converts from an {@code Integer} array to an {@code int} array.
     *
     * @param array {@code Integer} array
     * @return Equivalent {@code int} array
     */
    public static int[] asPrimitiveArray(Integer[] array) {
        int[] primitiveArray = new int[array.length];

        for (int i = 0; i < array.length; i++)
            primitiveArray[i] = array[i];

        return primitiveArray;
    }
}

Related

  1. getUniqueItems(final String[] allItems, final String newItem, final int maxItems)
  2. getUniqueWords(String[] input)
  3. invertRelabeling(int[] relabeling, int[] uniqueVars, int maxVarNum)
  4. unique(double[] in)
  5. unique(int[] a, int aLen, int[] b, int bLen)
  6. unique(Object[] elements)
  7. uniqueInts(int[] ints)
  8. uniques(final int[] ints)