Java Program to Print all unique elements of an array

Java Program to Print all unique elements of an array

In this tutorial you will be learning writing java program to print the all unique elements. Unique elements mean we don’t have to print duplicate elements of an array.

Array don’t restrict us to inserting duplicate value, but due to our business requirement we might need sometime all unique values.

For example:

Our input: arr[] = [2, 3, 9, 5, 2, 4, 3, 9]
Output will be : 2, 3, 9, 5, 4

Input: arr[] = [1, 2, 3, 4, 5, 5, 4, 3, 2]
Output will be : 1, 2, 3, 4, 5

Program 1 : Java Program to print unique element in array

  • This is a Simple Solution where we will be using two nested for loops.
  • The outer for loop will pick an element one by one starting from the beginning.
  • The inner for loop checks if the selected element is present on left side of it.
  • If present, then ignores the element as we have already printed that element.
  • If not present in leftmost then prints the element.
import java.util.Arrays;
import java.util.*; 
class Main
{
    public static void main(String []args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Java Program to print unique element of Array");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        } 
        System.out.print("Unique values in array are : ");
        for (int i = 0; i < size; i++)
        {
            int j;
            for (j = 0; j < i; j++)
            if (arr[i] == arr[j])
                break;
            if (i == j)
                System.out.print( arr[i] + " ");
        }
    }
}

Output

Java Program to print unique element of Array
Enter the size of array: 6
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 1
Please give value for index 4 : 2
Please give value for index 5 : 2
Unique values in array are : 1 2 3 

Program 2: Java Program to print unique elements in Array by Sorting

  • Here we will be using Sorting concept.
  • First sort the array using Array.sort().
  • By Sorting, all array elements become consecutive.
  • After sorting traverse the sorted array and print distinct/ unique elements.
import java.util.Arrays;
import java.util.*; 
class Main
{
    public static void main(String []args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Java Program to print unique element of Array");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        } 
        System.out.print("Unique values in array are : ");
        Arrays.sort(arr);
        for (int i = 0; i < size; i++)
        {
            while (i < size - 1 && arr[i] == arr[i + 1])
            i++;
            System.out.print(arr[i] +" ");
        }
    }
}

Output

Java Program to print unique element of Array
Enter the size of array: 7   
Please give value for index 0 : 4 
Please give value for index 1 : 1
Please give value for index 2 : 4
Please give value for index 3 : 2
Please give value for index 4 : 4
Please give value for index 5 : 5
Please give value for index 6 : 5
Unique values in array are : 1 2 4 5