Java Program To Find Unique Elements In An Array

Chapter: Miscellaneous Last Updated: 14-05-2023 12:00:10 UTC

Program:

            /* ............... START ............... */
                
import java.util.ArrayList;
import java.util.HashSet;

public class UniqueElementsFinder {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 2, 3, 5, 6, 1};

        int[] uniqueElements = findUniqueElements(array);

        System.out.println("Unique elements in the array: ");
        for (int element : uniqueElements) {
            System.out.print(element + " ");
        }
    }

    public static int[] findUniqueElements(int[] array) {
        HashSet<Integer> uniqueSet = new HashSet<>();
        ArrayList<Integer> uniqueList = new ArrayList<>();

        for (int element : array) {
            if (!uniqueSet.contains(element)) {
                uniqueSet.add(element);
                uniqueList.add(element);
            }
        }

        int[] uniqueArray = new int[uniqueList.size()];
        for (int i = 0; i < uniqueList.size(); i++) {
            uniqueArray[i] = uniqueList.get(i);
        }

        return uniqueArray;
    }
}

                /* ............... END ............... */
        

Output

Unique elements in the array:
1 2 3 4 5 6
The program finds the unique elements in the given array {1, 2, 3, 4, 2, 3, 5, 6, 1} and prints 
them in the order they appear. The unique elements in this case are 1, 2, 3, 4, 5, and 6.

Notes:

  • In this program, the findUniqueElements method takes an array as input and uses a HashSet and an ArrayList to find and store the unique elements. The HashSet is used to check if an element has already been encountered, and the ArrayList is used to maintain the order of the unique elements.
  • The findUniqueElements method returns an array containing the unique elements found. The main method demonstrates the usage of this method by creating an array, calling findUniqueElements, and printing the unique elements.
  • In summary, the program utilizes a HashSet to keep track of unique elements and an ArrayList to maintain the order of those elements. By iterating over the input array and checking for duplicates, the program creates a new array containing only the unique elements, which are then printed as the output.

Tags

Java program to find unique elements in an array, Java Program To Find Unique Elements In An Array

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018

1