Java Array Unique unique(double[] in)

Here you can find the source of unique(double[] in)

Description

Same behaviour as mathlab unique.

License

Open Source License

Parameter

Parameter Description
in the input array

Return

a sorted array with no duplicates values

Declaration

public static double[] unique(double[] in) 

Method Source Code

//package com.java2s;
/**/*  w ww.j  a va 2  s.com*/
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */

import java.util.Arrays;

public class Main {
    /**
     * Same behaviour as mathlab unique.
     * 
     * @param in  the input array
     * @return a sorted array with no duplicates values
     */
    public static double[] unique(double[] in) {
        Arrays.sort(in);
        int n = in.length;
        double[] temp = new double[n];
        temp[0] = in[0];
        int count = 1;
        for (int i = 1; i < n; i++) {
            if (Double.compare(in[i], in[i - 1]) != 0) {
                temp[count++] = in[i];
            }
        }
        if (count == n) {
            return temp;
        }
        return Arrays.copyOf(temp, count);
    }

    /**
     * Same behaviour as mathlab unique.
     * 
     * @param in  the input array
     * @return a sorted array with no duplicates values
     */
    public static int[] unique(int[] in) {
        Arrays.sort(in);
        int n = in.length;
        int[] temp = new int[n];
        temp[0] = in[0];
        int count = 1;
        for (int i = 1; i < n; i++) {
            if (in[i] != in[i - 1]) {
                temp[count++] = in[i];
            }
        }
        if (count == n) {
            return temp;
        }
        return Arrays.copyOf(in, count);
    }
}

Related

  1. countUnique(int[][] array)
  2. getUniqueIDsArray(int[] idArray, String ids, String seperator)
  3. getUniqueItems(final String[] allItems, final String newItem, final int maxItems)
  4. getUniqueWords(String[] input)
  5. invertRelabeling(int[] relabeling, int[] uniqueVars, int maxVarNum)
  6. unique(int[] a, int aLen, int[] b, int bLen)
  7. unique(int[] array)
  8. unique(Object[] elements)
  9. uniqueInts(int[] ints)