JAVA Solution Using (1).Array [15ms] || (2).HashMap [25ms] 
IntuitionThe challenge is to track how many elements of each row and column have been processed efficiently. This can be achieved by mapping the elements of mat
# Intuition
The challenge is to track how many elements of each row and column have been processed efficiently. This can be achieved by mapping the elements of mat to their positions by using HashMap or Array.And then we traverse the array maintaining frequency arrays of rows and columns. And we return the index of arr where the frequency of that index in rows = columns length or vice versa . 

# Approach
**1.Preprocessing the Matrix (mp array):**
Create a mapping mp where:
mp[val] stores the row and column indices of the value val in mat.
This allows you to directly access the position of any element in 
O(1) time.

**2.Tracking Rows and Columns:**
Use rows[i] to track the count of filled cells in the i-th row.
Use cols[j] to track the count of filled cells in the j-th column.

**3.Processing the Array (arr):**
For each element in arr, find its position using the mp array.
Increment the count for the corresponding row and column.
Check if the current row or column is completely filled:
If rows[i] == m, it means the i-th row is completely filled.
If cols[j] == n, it means the j-th column is completely filled.
Return the index of the current element in arr as soon as a row or column is completed.

**4.Dummy Return:**
Return -1.

# Complexity
- Time complexity: O(n⋅m+arr.length)

- Space complexity: o(n.m)
 

# Using Array
# Code
```java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int n=mat.length;
        int m=mat[0].length;
        int[][] mp = new int[m*n+1][2];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                mp[mat[i][j]]=new int[]{i,j};
            }
        }
        int[] rows = new int[n];
        int[] cols = new int[m];
        for(int i=0;i<arr.length;i++){
            int[] pos = mp[arr[i]];
            rows[pos[0]]++;
            cols[pos[1]]++;
            if((rows[pos[0]]==m)||(cols[pos[1]]==n)){
                return i;
            }
        }
        return -1;
    }
}
```
# Using HashMap
# Complexity
- Time complexity: O(n⋅m+arr.length)

- Space complexity: o(n.m)

<!-- Add your space complexity here, e.g. $$O(n)$$ -->
```
java
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        HashMap<Integer,int[]> mp = new HashMap<>();
        int n=mat.length;
        int m=mat[0].length;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                mp.put(mat[i][j],new int[]{i,j});
            }
        }
        int[] rows = new int[n];
        int[] cols = new int[m];
        for(int i=0;i<arr.length;i++){
            int[] pos = mp.get(arr[i]);
            rows[pos[0]]++;
            cols[pos[1]]++;
            if((rows[pos[0]]==m)||(cols[pos[1]]==n)){
                return i;
            }
        }
        return -1;
    }
}
``` 
['Array', 'Hash Table', 'Java']