diff --git a/Duplicates in an Array_java b/Duplicates in an Array_java new file mode 100644 index 00000000..88f7fd06 --- /dev/null +++ b/Duplicates in an Array_java @@ -0,0 +1,36 @@ +import java.util.HashSet; +import java.util.Scanner; + +public class DuplicateCheck { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.print("Enter the number of elements: "); + int n = sc.nextInt(); + + int[] arr = new int[n]; + System.out.println("Enter " + n + " elements:"); + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + + HashSet seen = new HashSet<>(); + boolean hasDuplicate = false; + + for (int num : arr) { + if (seen.contains(num)) { + hasDuplicate = true; + break; + } + seen.add(num); + } + + if (hasDuplicate) { + System.out.println("Duplicate elements found in the array."); + } else { + System.out.println("No duplicates found."); + } + + sc.close(); + } +}