Type-1:
Here's a Java program to remove duplicate elements from an array of integers, along with an explanation of each step:
import java.util.Arrays;
import java.util.LinkedHashSet;
public class RemoveDuplicatesFromArray {
public static void main(String[] args) {
// Step 1: Define an integer array with some duplicate elements
int[] numbers = { 1, 2, 3, 2, 4, 5, 1, 6, 3 };
// Step 2: Call the function to remove duplicates
int[] uniqueNumbers = removeDuplicates(numbers);
// Step 3: Print the array without duplicates
System.out.println("Array without duplicates: ");
for (int number : uniqueNumbers) {
System.out.print(number + " ");
}
}
// Function to remove duplicates from the array
public static int[] removeDuplicates(int[] arr) {
// Step 4: Use LinkedHashSet to remove duplicates while preserving order
LinkedHashSet<Integer> set = new LinkedHashSet<>();
for (int number : arr) {
set.add(number);
}
// Step 5: Convert the LinkedHashSet back to an array
int[] uniqueArray = new int[set.size()];
int index = 0;
for (int number : set) {
uniqueArray[index++] = number;
}
// Step 6: Return the array without duplicates
return uniqueArray;
}
}
Explanation of each step:
- We start by defining an integer array named numbers. In this example, we have an array with elements {1, 2, 3, 2, 4, 5, 1, 6, 3}. This array contains some duplicate elements.
- We call the function removeDuplicates(numbers) to remove duplicates from the array numbers.
- We then print the array without duplicates using a for-each loop to iterate through each element and display it.
- The function removeDuplicates takes an integer array as input and returns an array without duplicate elements.
- Inside the function, we use a LinkedHashSet named set. A LinkedHashSet is a collection that maintains the insertion order of elements and automatically removes duplicates. We iterate through the input array arr, and for each element, we add it to the set.
- After adding all elements to the set, it contains only unique elements, and any duplicates have been removed while preserving the order of the original array.
- Next, we create a new integer array named uniqueArray with a size equal to the number of unique elements in the set.
- We use a traditional for loop to convert the set back to an array. The loop iterates through the elements of the set and assigns them to the corresponding positions in the uniqueArray.
- Finally, the function returns the uniqueArray, which contains the elements of the original array without any duplicates.
When you run this program with the provided array, it will output:
Array without duplicates:
1 2 3 4 5 6
This is because the duplicate elements {1, 2, 3} have been removed from the original array, resulting in the new array {1, 2, 3, 4, 5, 6}. If you modify the array elements, the program will remove duplicates and display the new array accordingly.
Type-2:
Another way to remove duplicate elements from an array of integers is to use a nested for loop to compare each element with all subsequent elements and remove duplicates in-place. Here's the Java program using this approach:
import java.util.Arrays;
public class RemoveDuplicatesFromArray {
public static void main(String[] args) {
// Step 1: Define an integer array with some duplicate elements
int[] numbers = { 1, 2, 3, 2, 4, 5, 1, 6, 3 };
// Step 2: Call the function to remove duplicates
int[] uniqueNumbers = removeDuplicates(numbers);
// Step 3: Print the array without duplicates
System.out.println("Array without duplicates: ");
for (int number : uniqueNumbers) {
System.out.print(number + " ");
}
}
// Function to remove duplicates from the array
public static int[] removeDuplicates(int[] arr)
{
int n = arr.length;
int uniqueElements = n;
// Step 4: Nested for loop to remove duplicates in-place
for (int i = 0; i < uniqueElements; i++) {
for (int j = i + 1; j < uniqueElements; j++) {
if (arr[i] == arr[j]) {
// Found a duplicate, remove it by shifting elements
for (int k = j; k < uniqueElements - 1; k++) {
arr[k] = arr[k + 1];
}
uniqueElements--;
j--;
}
}
}
// Step 5: Create a new array with unique elements and copy elements to it
int[] uniqueArray = Arrays.copyOf(arr, uniqueElements);
// Step 6: Return the array without duplicates
return uniqueArray;
}
}
Explanation of each step:
- We start by defining an integer array named numbers, just like in the previous example.
- We call the function removeDuplicates(numbers) to remove duplicates from the array numbers.
- We then print the array without duplicates using a for-each loop to iterate through each element and display it.
- The function removeDuplicates takes an integer array as input and returns an array without duplicate elements.
- Inside the function, we use a nested for loop to compare each element with all subsequent elements in the array. If we find a duplicate, we remove it by shifting the subsequent elements one position to the left. This is done in-place without using any additional data structures.
- We use a variable uniqueElements to keep track of the number of unique elements in the array. It starts with the total number of elements in the original array (n).
- Whenever we find a duplicate at index j, we shift all subsequent elements one position to the left, effectively overwriting the duplicate element.
- We also decrement uniqueElements by 1 since we have removed a duplicate element.
- After removing all duplicates in-place, we create a new array named uniqueArray with the length of uniqueElements using Arrays.copyOf(), and we copy the unique elements from the modified arr to uniqueArray.
- Finally, the function returns the uniqueArray, which contains the elements of the original array without any duplicates.
When you run this program with the provided array, it will output:
Array without duplicates:
1 2 3 4 5 6
This is because the duplicate elements {1, 2, 3} have been removed from the original array, resulting in the new array {1, 2, 3, 4, 5, 6}. If you modify the array elements, the program will remove duplicates and display the new array accordingly.
0 Comments