Introduction
Removing duplicates from an array is a common programming task, which helps in ensuring that the data set contains only unique elements. This guide will walk you through writing a Java program that removes duplicates from a given array and returns the array with unique elements only.
Problem Statement
Create a Java program that:
- Prompts the user to enter the size of an array and its elements.
- Removes any duplicate elements from the array.
- Displays the array with unique elements.
Example:
- Input:
[1, 2, 2, 3, 4, 4, 5] - Output:
"Array after removing duplicates: [1, 2, 3, 4, 5]"
Solution Steps
- Read the Array Size and Elements: Use the
Scannerclass to take the size and elements of the array as input from the user. - Use a Data Structure to Track Unique Elements: Utilize a
Setto store unique elements. - Traverse the Array: Iterate through the array and add each element to the
Set. - Convert the Set Back to an Array: Convert the
Setback to an array. - Display the Result: Print the array with unique elements.
Java Program
// Java Program to Remove Duplicates from an Array
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
import java.util.Set;
import java.util.LinkedHashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
// Step 1: Read the size and elements of the array from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
// Step 2: Remove duplicates using a Set
int[] uniqueArray = removeDuplicates(array);
// Step 3: Display the array with unique elements
System.out.println("Array after removing duplicates:");
for (int i = 0; i < uniqueArray.length; i++) {
System.out.print(uniqueArray[i] + " ");
}
}
// Method to remove duplicates from the array
public static int[] removeDuplicates(int[] array) {
// Use a LinkedHashSet to maintain the order of elements
Set<Integer> set = new LinkedHashSet<>();
// Step 3: Traverse the array and add elements to the set
for (int i = 0; i < array.length; i++) {
set.add(array[i]);
}
// Step 4: Convert the set back to an array
int[] uniqueArray = new int[set.size()];
int index = 0;
for (int element : set) {
uniqueArray[index++] = element;
}
return uniqueArray;
}
}
Explanation
Step 1: Read the Array Size and Elements
- The
Scannerclass is used to read the size of the array and its elements. ThenextInt()method captures the size and each element.
Step 2: Use a Data Structure to Track Unique Elements
- A
LinkedHashSetis used to store the elements of the array.LinkedHashSetis chosen because it maintains the insertion order and automatically handles duplicates.
Step 3: Traverse the Array
- A
forloop is used to iterate through each element of the array, and each element is added to theSet. SinceSetdoes not allow duplicate elements, it effectively removes duplicates.
Step 4: Convert the Set Back to an Array
- After removing duplicates, the
Setis converted back to an array.
Step 5: Display the Result
- The program prints the array with unique elements using a
forloop.
Output Example
Example 1:
Enter the size of the array: 7
Enter the elements of the array:
1 2 2 3 4 4 5
Array after removing duplicates:
1 2 3 4 5
Example 2:
Enter the size of the array: 5
Enter the elements of the array:
10 20 20 30 30
Array after removing duplicates:
10 20 30
Example 3:
Enter the size of the array: 6
Enter the elements of the array:
1 1 1 1 1 1
Array after removing duplicates:
1
Conclusion
This Java program demonstrates how to remove duplicates from an array using a Set to ensure that only unique elements are retained. This solution leverages the properties of Set to efficiently remove duplicates, making it a valuable exercise for understanding data structures and basic array manipulation in Java programming.