How do I create array of unique values from another array?

This code snippet show you how to create an array of unique numbers from another array of numbers. The example is taken from the comment posted by Mednikov Yury in relation to the following example: How do I remove duplicate element from array?

package org.kodejava.example.lang;

public class UniqueArray {
    /**
     * Return true if number num is appeared only once in the
     * array – num is unique.
     */
    public static boolean isUnique(int[] array, int num) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == num) {
                return false;
            }
        }
        return true;
    }

    /**
     * Convert the given array to an array with unique values –
     * without duplicates and Return it
     */
    public static int[] toUniqueArray(int[] array) {
        int[] temp = new int[array.length];

        for (int i = 0; i < temp.length; i++) {
            temp[i] = -1; // in case u have value of 0 in he array
        }
        int counter = 0;

        for (int i = 0; i < array.length; i++) {
            if (isUnique(temp, array[i]))
                temp[counter++] = array[i];
        }
        int[] uniqueArray = new int[counter];

        System.arraycopy(temp, 0, uniqueArray, 0, uniqueArray.length);

        return uniqueArray;
    }

    /**
     * Print given array
     */
    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

        System.out.println("");
    }

    public static void main(String[] args) {
        int[] array = {1, 1, 2, 3, 4, 1, 4, 7, 9, 7};
        printArray(array);
        printArray(toUniqueArray(array));
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

2 Comments

Leave a Reply