Skip to content

Commit d1b9cda

Browse files
Added Linear Search for 2D Array
1 parent e499d3b commit d1b9cda

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Arrays;
2+
3+
public class LinearSearch2DArray {
4+
public static void main(String[] args) {
5+
int[][] array = new int[][]{{1,2,3,4,5},
6+
{6,7,8,9,10},
7+
{11,12,13,14}};
8+
9+
int x = 8;
10+
11+
int[] index = Linearsearch(array, x);
12+
System.out.println("The element is found at "+ Arrays.toString(index));
13+
}
14+
15+
public static int[] Linearsearch(int[][] array, int x){
16+
for (int i = 0; i < array.length; i++){
17+
for (int j = 0; j < array[i].length; j++){
18+
if (array[i][j] == x){
19+
return new int[]{i,j};
20+
}
21+
}
22+
}
23+
return new int[]{-1,-1};
24+
}
25+
}

0 commit comments

Comments
 (0)