We can find the unique values in an array using a HashSet in Java. Since a HashSet does not allow duplicate elements, adding all array values to a HashSet will automatically remove duplicates.
We have added values of array to set using for loop to obtain only the unique values.
Below is an example demonstrating this approach using Java with Selenium.
Example Automation Script:
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import org.testng.annotations.Test;
public class ExceptionCheck {
@Test
public void findUniqueValues() {
Integer[] arr = {3, 2, 3, 4, 2, 5};
Set<Integer> uniqueValues = new HashSet<>();
for(int val:arr) {
uniqueValues.add(val);
}
System.out.println(uniqueValues);
}
}