-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search Element from ArrayList
59 lines (46 loc) · 1.8 KB
/
Search Element from ArrayList
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
48
49
50
51
52
53
54
55
56
57
58
59
import java.lang.*;
import java.util.*;
public class Main {
public static boolean searchElementinArray(ArrayList<Integer> a, int b){
int aLen = a.size();
for(int i=0; i<aLen; i++){
if(a.get(i) == b){
return true;
}
}
return false;
}
public static void printArray(ArrayList<Integer> arr){
int arrLen = arr.size();
for(int i=0; i<arrLen; i++){
System.out.print(arr.get(i)+" ");
}
System.out.println();
}
public static void main(String[] args) {
// YOUR CODE GOES HERE
// Please take input and print output to standard input/output (stdin/stdout)
// DO NOT USE ARGUMENTS FOR INPUTS
// E.g. 'Scanner' for input & 'System.out' for output
Scanner sc = new Scanner(System.in);
int T = sc.nextInt(); // number of test cases
System.out.println("T: "+T);
for(int i=1; i<=T; i++){ // outer loop for test cases
ArrayList<Integer> arr = new ArrayList<Integer>(); // declaring Array list
int arrLength = sc.nextInt(); // getting input of array lenght
System.out.println("arrLength: "+arrLength);
for(int j=0; j<arrLength; j++){ // inner loop for arrat to add values
arr.add(sc.nextInt()); // adding value to array list from input
}
printArray(arr);
int findVal = sc.nextInt(); // getting input value which needs to be find in an array
System.out.println("findVal: "+findVal);
if(searchElementinArray(arr, findVal)){ // check point and function call will return boolean value
System.out.println(1); // if function return true then print 1
}
else{
System.out.println(0); // if function return flase then print 0
}
}
}
}