From 681835631eb0c9deaac396f219268c924f3be63c Mon Sep 17 00:00:00 2001 From: kl2400030727 Date: Fri, 31 Oct 2025 22:38:04 +0530 Subject: [PATCH] Create Duplicates in an Array_java --- Duplicates in an Array_java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Duplicates in an Array_java 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(); + } +}