From d1b9cda3249c063ad2183fc552967e975468cbac Mon Sep 17 00:00:00 2001 From: Ashish Singh <94695243+Ashish-9455187250@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:57:12 +0530 Subject: [PATCH] Added Linear Search for 2D Array --- .../searches/LinearSearch2DArray | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/LinearSearch2DArray diff --git a/src/main/java/com/thealgorithms/searches/LinearSearch2DArray b/src/main/java/com/thealgorithms/searches/LinearSearch2DArray new file mode 100644 index 000000000000..9c5f0a3c4055 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/LinearSearch2DArray @@ -0,0 +1,25 @@ +import java.util.Arrays; + +public class LinearSearch2DArray { + public static void main(String[] args) { + int[][] array = new int[][]{{1,2,3,4,5}, + {6,7,8,9,10}, + {11,12,13,14}}; + + int x = 8; + + int[] index = Linearsearch(array, x); + System.out.println("The element is found at "+ Arrays.toString(index)); + } + + public static int[] Linearsearch(int[][] array, int x){ + for (int i = 0; i < array.length; i++){ + for (int j = 0; j < array[i].length; j++){ + if (array[i][j] == x){ + return new int[]{i,j}; + } + } + } + return new int[]{-1,-1}; + } +}