Answer:
Here is an implementation of the hasDuplicates method in the U6_L2_Activity_Three class:
import java.util.HashSet;
public class U6_L2_Activity_Three {
public static boolean hasDuplicates(int[] array) {
// Create a HashSet to store the values in the array
HashSet<Integer> set = new HashSet<>();
// Iterate through the array and add each value to the set
for (int value : array) {
if (set.contains(value)) {
// If the value is already in the set, return true
return true;
}
set.add(value);
}
// If no duplicates were found, return false
return false;
}
}
To test this method, you can use the Runner class like this:
public class Runner {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {1, 2, 2, 3, 4, 5};
int[] array3 = {1, 1, 2, 3, 4, 5};
System.out.println(U6_L2_Activity_Three.hasDuplicates(array1)); // should print false
System.out.println(U6_L2_Activity_Three.hasDuplicates(array2)); // should print true
System.out.println(U6_L2_Activity_Three.hasDuplicates(array3)); // should print true
}
}
Step-by-step explanation:
This will print false, true, and true to the console, indicating that the hasDuplicates method is working as expected.