forked from LogicFirstTamil/DSA-in-JAVA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearSearch.java
76 lines (62 loc) · 1.55 KB
/
LinearSearch.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public class LinearSearch {
/* returns the index of target
* returns -1 if not found
*/
public static int find(int[] arr,int target) {
for(int i=0;i<arr.length;i++)
if(arr[i]==target)
return i;
return -1;
}
/* returns true if target is found
* returns false if not found
*/
public static boolean contains(int[] arr,int target) {
for(int i=0;i<arr.length;i++)
if(arr[i]==target)
return true;
return false;
}
/* Search for a char in a string
* returns the index of target
* returns -1 if not found
*/
public static int find(String str,char target) {
for(int i=0;i<str.length();i++)
if(str.charAt(i)==target)
return i;
return -1;
}
/* returns true if target is found
* returns false if not found
*/
public static boolean contains(String str,char target) {
for(int i=0;i<str.length();i++)
if(str.charAt(i)==target)
return true;
return false;
}
/* Search 2d array
* returns the index of target in an int array [row,col]
* returns [-1,-1] if not found
*/
public static int[] find(int[][] arr, int target) {
for(int row=0;row<arr.length;row++) {
for(int col=0;col<arr[row].length;col++) {
if(arr[row][col]==target)
return new int[] {row,col};
}
}
return new int[] {-1,-1};
}
}
/*
* Assignment
*
* Find max element
* Find Min element
* Count a character in a string - count(str,'r')
* Count the 4 digit elements in an array - {123,7845,124,78452,1429}
*
*
*/