Java Array Sort getSortedDistinct(int[] values)

Here you can find the source of getSortedDistinct(int[] values)

Description

Sort array & return array with removed repetitive values.

License

Open Source License

Parameter

Parameter Description
values input array (this method will quickSort this array)

Return

sorted array of distinct values

Declaration

public static int[] getSortedDistinct(int[] values) 

Method Source Code


//package com.java2s;
/*//from  www.  j  a v a 2s.  co  m
 * Redberry: symbolic tensor computations.
 *
 * Copyright (c) 2010-2013:
 *   Stanislav Poslavsky   <stvlpos@mail.ru>
 *   Bolotin Dmitriy       <bolotin.dmitriy@gmail.com>
 *
 * This file is part of Redberry.
 *
 * Redberry is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Redberry is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Redberry. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;

public class Main {
    /**
     * Sort array & return array with removed repetitive values.
     *
     * @param values input array (this method will quickSort this array)
     *
     * @return sorted array of distinct values
     */
    public static int[] getSortedDistinct(int[] values) {
        if (values.length == 0)
            return values;
        Arrays.sort(values);
        int shift = 0;
        int i = 0;
        while (i + shift + 1 < values.length)
            if (values[i + shift] == values[i + shift + 1])
                ++shift;
            else {
                values[i] = values[i + shift];
                ++i;
            }
        values[i] = values[i + shift];
        return Arrays.copyOf(values, i + 1);
    }
}

Related

  1. difference(int[] sorted1, int[] sorted2)
  2. extractFields(String[] items, int[] fields, String delim, boolean sortKeyFields)
  3. getLeftIndex(float[] sorted, float value)
  4. getMedianIndex(float[] sorted, float value)
  5. getNewSortedIntArray(int[] codePointArray)
  6. intArrayToShortArraySorted(int[] source)
  7. multiQuickSort(int[]... arrays)
  8. quickSort(int[] arr, int startIndex, int endIndex)
  9. radixSort(int[] vs)