-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateElement.java
More file actions
47 lines (39 loc) · 1.4 KB
/
Copy pathDuplicateElement.java
File metadata and controls
47 lines (39 loc) · 1.4 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
public class DuplicateElement {
static void findDuplicateElements(int arr[]){
boolean visited[]= new boolean[arr.length];
for(int i=0; i<arr.length; i++){
//SKIP IF ELEMENT IS ALREADY CHECKED
if(visited[i]){
continue;
}
int count=0, first_index=0, last_index=0;
for(int j=i+1; j<arr.length; j++) {
if (arr[i] == arr[j]) {
visited[j]=true; //MARK THIS ELEMENT AS CHECKED
count++;
last_index=j;
System.out.print(j+ " ");
}
}
if(count>0){
System.out.println("(Index position), " + arr[i] + " duplicates " + count + " times");
System.out.println("First Index: " + i);
System.out.println("Last Index: " + last_index);
System.out.println();
}
}
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
//ARRAY SIZE AND ELEMENTS INPUT
System.out.print("Enter size of array: ");
int size= sc.nextInt();
int arr[]= new int[size];
System.out.print("Enter " + size + " numbers: ");
for(int i=0; i<arr.length; i++){
arr[i]=sc.nextInt();
}
findDuplicateElements(arr);
}
}