-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicates_InArray.java
More file actions
27 lines (26 loc) · 1004 Bytes
/
Duplicates_InArray.java
File metadata and controls
27 lines (26 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// WAP to find the duplicate elements in an Array
import java.util.Scanner;
public class Duplicates_InArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the Array: ");
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int [] counts = new int[100]; // Assuming the array elements are in the range 0-99
System.out.print("Duplicate elements are: ");
boolean foundDuplicates = false;
for (int i = 0; i < n; i++) {
counts[arr[i]]++; // Increment the count for each element
if (counts[arr[i]] == 2) { // Print only when the count becomes 2
System.out.print(arr[i] + " ");
foundDuplicates = true;
}
}
if(!foundDuplicates){
System.out.println("None");
}
}
}