-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateElement_CountAndPosition.java
More file actions
42 lines (38 loc) · 1.42 KB
/
Copy pathDuplicateElement_CountAndPosition.java
File metadata and controls
42 lines (38 loc) · 1.42 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
import java.util.Scanner;
public class DuplicateElement_CountAndPosition {
//METHOD TO COUNT OCCURRENCE OF TARGET ELEMENT, PRINT ITS INDEX POSITIONS AND FIRST AND LAST POSITION OF ITS OCCURRENCE
static void countAndPosition(int arr[], int target){
int count=0, pos=0;
System.out.print("Positions at which " + target + " occurred= ");
for(int i=0; i<arr.length; i++){
if(arr[i]==target){
count++;
pos= i;
System.out.print(pos+ " ");
}
}
System.out.println();
for(int i=0; i<arr.length; i++){
if(arr[i]==target){
System.out.println("First position= " + i);
break;
}
}
System.out.println("No. of times " + target + " occurred= " + count);
System.out.println("Last position= " + pos);
}
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 array elements: ");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter a number to count its occurrence and return position: ");
int target= sc.nextInt();
countAndPosition(arr, target);
}
}