Java Array Remove removeRepeats(T[] array)

Here you can find the source of removeRepeats(T[] array)

Description

Removes any repeating data from the array

License

Open Source License

Parameter

Parameter Description
array The array to remove repeats from

Return

The new array

Declaration

public static <T> T[] removeRepeats(T[] array) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    /**/*from   w ww .jav  a 2  s  .c om*/
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static <T> T[] removeRepeats(T[] array) {
        List<T> cache = new ArrayList<T>();
        for (T o : array)
            if (!cache.contains(o))
                cache.add(o);
        return cache.toArray(Arrays.copyOf(array, cache.size()));
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static byte[] removeRepeats(byte[] array) {
        List<Byte> cache = new ArrayList<Byte>();
        for (Byte o : array)
            if (!cache.contains(o))
                cache.add(o);
        Byte[] objArray = cache.toArray(new Byte[cache.size()]);
        byte[] newArray = new byte[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static short[] removeRepeats(short[] array) {
        List<Short> cache = new ArrayList<Short>();
        for (Short o : array)
            if (!cache.contains(o))
                cache.add(o);
        Short[] objArray = cache.toArray(new Short[cache.size()]);
        short[] newArray = new short[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static int[] removeRepeats(int[] array) {
        List<Integer> cache = new ArrayList<Integer>();
        for (Integer o : array)
            if (!cache.contains(o))
                cache.add(o);
        Integer[] objArray = cache.toArray(new Integer[cache.size()]);
        int[] newArray = new int[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static long[] removeRepeats(long[] array) {
        List<Long> cache = new ArrayList<Long>();
        for (Long o : array)
            if (!cache.contains(o))
                cache.add(o);
        Long[] objArray = cache.toArray(new Long[cache.size()]);
        long[] newArray = new long[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static float[] removeRepeats(float[] array) {
        List<Float> cache = new ArrayList<Float>();
        for (Float o : array)
            if (!cache.contains(o))
                cache.add(o);
        Float[] objArray = cache.toArray(new Float[cache.size()]);
        float[] newArray = new float[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static double[] removeRepeats(double[] array) {
        List<Double> cache = new ArrayList<Double>();
        for (Double o : array)
            if (!cache.contains(o))
                cache.add(o);
        Double[] objArray = cache.toArray(new Double[cache.size()]);
        double[] newArray = new double[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static boolean[] removeRepeats(boolean[] array) {
        List<Boolean> cache = new ArrayList<Boolean>();
        for (Boolean o : array)
            if (!cache.contains(o))
                cache.add(o);
        Boolean[] objArray = cache.toArray(new Boolean[cache.size()]);
        boolean[] newArray = new boolean[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }

    /**
     * Removes any repeating data from the array
     * @param array The array to remove repeats from
     * @return The new array
     */
    public static char[] removeRepeats(char[] array) {
        List<Character> cache = new ArrayList<Character>();
        for (Character o : array)
            if (!cache.contains(o))
                cache.add(o);
        Character[] objArray = cache.toArray(new Character[cache.size()]);
        char[] newArray = new char[objArray.length];
        for (int i = 0; i < objArray.length; i++)
            newArray[i] = objArray[i];
        return newArray;
    }
}

Related

  1. removeNulls(T[] array)
  2. removeNulls(T[] input)
  3. removeNullsFromStringArray(String[] array)
  4. removeOwnCookieNameFromCookieHeader(String ownCookieName, String[] cookies)
  5. removePointerFromDesc(String[] desc)
  6. removeRows(int[][] array, Collection rowsIndices)
  7. removeTrailing(final byte[] id)
  8. removeUser(String[] array, String user)
  9. removeValues(int[] values, int[] valuesToRemove)