Introduction
Removing duplicate elements from an array is a common task in programming. There are multiple ways to achieve this in Java, each with its own benefits and complexity. In this guide, we'll explore different methods to remove duplicates from an array using Java.
Table of Contents
- Using a Temporary Array
- Using a Set
- Using Streams (Java 8 and above)
- Example Programs with Output
1. Using a Temporary Array
This method involves sorting the array first and then using a temporary array to store unique elements. This approach requires extra space for the temporary array.
Example:
import java.util.Arrays;
public class RemoveDuplicatesUsingTempArray {
public static void main(String[] args) {
int[] array = {1, 2, 2, 3, 4, 4, 5};
// Remove duplicates using a temporary array
int[] uniqueArray = removeDuplicates(array);
// Print the unique elements
System.out.println("Array after removing duplicates: " + Arrays.toString(uniqueArray));
}
public static int[] removeDuplicates(int[] array) {
Arrays.sort(array); // Sort the array first
int[] temp = new int[array.length];
int j = 0;
// Traverse the sorted array and store unique elements in temp
for (int i = 0; i < array.length - 1; i++) {
if (array[i] != array[i + 1]) {
temp[j++] = array[i];
}
}
temp[j++] = array[array.length - 1];
// Copy the unique elements to the final array
int[] result = new int[j];
System.arraycopy(temp, 0, result, 0, j);
return result;
}
}
Output:
Array after removing duplicates: [1, 2, 3, 4, 5]
2. Using a Set
Using a Set
is a straightforward way to remove duplicates because sets inherently do not allow duplicate elements. This approach is simple and efficient.
Example:
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class RemoveDuplicatesUsingSet {
public static void main(String[] args) {
Integer[] array = {1, 2, 2, 3, 4, 4, 5};
// Remove duplicates using a Set
Integer[] uniqueArray = removeDuplicates(array);
// Print the unique elements
System.out.println("Array after removing duplicates: " + Arrays.toString(uniqueArray));
}
public static Integer[] removeDuplicates(Integer[] array) {
Set<Integer> set = new LinkedHashSet<>(Arrays.asList(array));
return set.toArray(new Integer[0]);
}
}
Output:
Array after removing duplicates: [1, 2, 3, 4, 5]
3. Using Streams (Java 8 and above)
Java 8 introduced Streams, which provide a concise and functional approach to processing collections of objects. Streams can be used to remove duplicates from an array.
Example:
import java.util.Arrays;
import java.util.stream.Collectors;
public class RemoveDuplicatesUsingStreams {
public static void main(String[] args) {
Integer[] array = {1, 2, 2, 3, 4, 4, 5};
// Remove duplicates using streams
Integer[] uniqueArray = removeDuplicates(array);
// Print the unique elements
System.out.println("Array after removing duplicates: " + Arrays.toString(uniqueArray));
}
public static Integer[] removeDuplicates(Integer[] array) {
return Arrays.stream(array)
.distinct()
.toArray(Integer[]::new);
}
}
Output:
Array after removing duplicates: [1, 2, 3, 4, 5]
Conclusion
In this guide, we explored different ways to remove duplicate elements from an array in Java:
- Using a Temporary Array: A traditional approach that involves sorting and using extra space for a temporary array.
- Using a Set: A straightforward and efficient method utilizing the properties of sets.
- Using Streams: A modern and concise approach introduced in Java 8.
Each method has its own advantages and use cases. Understanding these techniques will help you choose the best approach based on your specific requirements and improve your problem-solving skills in Java.
Hello sir,
ReplyDeleteWhat is the implication of using the final keyword here with the arrays?
I tried without final keyword and it works fine.
It is just to make sure that we are not assigning value to array again.
DeleteIs it correct?
ReplyDeleteThis program is correct.
Deletethanks Sir
ReplyDeletein the Array solution, why was array a assigned values from array temp. what was the purpose?
ReplyDelete