Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This Java code defines a method findMissingAndRepeatedValues that takes a 2D integer array grid as input and returns an array of two integers: one representing a missing value and the other representing a repeated value in the grid.

A HashMap is used to keep track of the frequency of each element in the grid.

An array res of size 2 is initialized to store the result.

Iterate through each element of the grid.

If the element already exists in the map, increment its count.

If the element is not in the map, add it with a count of 1.

Iterate through all possible values from 1 to grid.length * grid.length.

If a value is not found in the map, it is the missing value and stored in res[1].

If a value appears twice in the map, it is the repeated value and stored in res[0].

This method efficiently finds one missing value and one repeated value in the provided grid by leveraging a HashMap to keep track of element frequencies and iterating through all possible values.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public int[] findMissingAndRepeatedValues(int[][] grid) {
Map<Integer,Integer> map=new HashMap<>();
int[] res=new int[2];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid.length;j++){
if(map.containsKey(grid[i][j])){
map.put(grid[i][j],map.get(grid[i][j])+1);
}
else{
map.put(grid[i][j],1);
}
}
}
for(int i=1;i<=grid.length*grid.length;i++){
if(!map.containsKey(i)){
res[1]=i;
}
else{
if(map.get(i)==2){
res[0]=i;
}
}
}
return res;
}
}