This example demonstrates you how to remove duplicate elements from an array using the help of java.util.HashSet
class.
package org.kodejava.util;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ArrayRemoveDuplicate {
public static void main(String[] args) {
// A string array with duplicate values.
String[] data = {"A", "C", "B", "D", "A", "B", "E", "D", "B", "C"};
System.out.println("Original array : " + Arrays.toString(data));
// Convert a string array into java.util.List because we need a list
// object to create the java.util.Set object.
List<String> list = Arrays.asList(data);
// A set is a collection object that cannot have a duplicate values,
// by converting the array to a set the duplicate value will be removed.
Set<String> set = new HashSet<>(list);
// Convert the java.util.Set back to array using the toArray() method of
// the set object copy the value in the set to the defined array.
String[] result = set.toArray(new String[0]);
System.out.println("Array with no duplicate: " + Arrays.toString(result));
}
}
The result of the code snippet above:
Original array : [A, C, B, D, A, B, E, D, B, C]
Array with no duplicate: [A, B, C, D, E]
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024