-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckIfKeyPresent
More file actions
38 lines (33 loc) · 949 Bytes
/
CheckIfKeyPresent
File metadata and controls
38 lines (33 loc) · 949 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
28
29
30
31
32
33
34
35
36
37
38
package Udemyprograms;
public class ArraysGeeksForGeeks4 {
public static boolean ifKeyPresent(int[] arr, int key, int size) {
/** initialise counter =0,res for checking whether we reached the no of size iteration
* iterate a loop
* inside loop check if the element is key
* if yes make res true
* if check we reached the size
* if yes check whether the res is true make it false;
* if yes continue iteration
*/
int counter = 0;
boolean res = false;
for (int i = 0; i < arr.length; i++) {
counter++;
if (arr[i] == key) {
res = true;
}
if (counter == size || i == arr.length - 1) {
if (!res) {
return false;
}
res = false;
counter = 0;
}
}
return true;
}
public static void main(String args[]) {
int arr[] = { 5, 8, 7, 12, 14, 3, 9};
System.out.println(ifKeyPresent(arr, 8, 2));
}
}