Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Duplicates in an Array_java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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();
}
}