Suppose I have a 3*3 array(2D array):
1 2 4
1 5 7
2 4 8
So 1,2 and 4 appear more than once. The result I want to get is 3. How can I do that?
Suppose I have a 3*3 array(2D array):
1 2 4
1 5 7
2 4 8
So 1,2 and 4 appear more than once. The result I want to get is 3. How can I do that?
Here's a brute-force straight forward way of counting duplicates. Turn the 2d array into a 1d array (List<Integer>
), then loop through the 1d array counting the duplicates as you find them and removing them so you don't count them more than once.
public static void main(String[] args) throws Exception {
int[][] _2dArray = {
{ 1, 2, 4 },
{ 1, 5, 7 },
{ 2, 4, 8 }
};
// Convert to a flat 1d array
List<Integer> flatArray = new ArrayList();
for (int[] row : _2dArray) {
for (int col : row) {
flatArray.add(col);
}
}
// Count and remove duplicates as you find them
int dupCount = 0;
for (int i = 0; i < flatArray.size(); i++) {
boolean dupFound = false;
for (int j = i + 1; j < flatArray.size(); j++) {
if (flatArray.get(i) == flatArray.get(j)) {
dupFound = true;
// Remove duplicate found in inner loop
flatArray.remove(j);
j--;
}
}
if (dupFound) {
dupCount++;
// Remove duplicate found in outer loop
flatArray.remove(i);
i--;
}
}
System.out.println(dupCount);
}
Results:
3
If you're interested in streams you can go this route also. It groups together all the numbers in the array into a map where the number is the key and the frequency of that number is the value. We then keep only the key/value pairs who have a value > 1 (The duplicates).
public static void main(String[] args) throws Exception {
int[][] _2dArray = {
{ 1, 2, 4 },
{ 1, 5, 7 },
{ 2, 4, 8 }
};
// Convert to a flat 1d array
int[] flatArray = Arrays.stream(_2dArray)
.flatMapToInt(Arrays::stream)
.toArray();
//Count duplicates
Object[] duplicates = Arrays.stream(flatArray).boxed()
// Group all integers together
.collect(Collectors.groupingBy(i -> i, Collectors.counting()))
// Keep key/value pairs whose value > 1
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1)
.toArray();
System.out.println(duplicates.length);
}
Results:
3
Here is the solution to find count of duplicate elements in a 2D matrix without using collection public class DuplicateElementsCountIn2DMetrixWithoutUsingCollections{
public static void main(String args[]){
String[][] matrix = {{"1","2","1","3",},{"7","6","null","2",},{"5","6","3","null",},{"3","10","9","5",}};
System.out.println(findDuplicate(matrix));
}
public static int findDuplicate(String[][] matrix){
String strArr[] = new String[(matrix.length)*(matrix[0].length)];
int count = 0;
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length; j++){
for(int k = 0; k < matrix.length; k++){
for(int l = 0; l < matrix[0].length; l++){
if((i!=k || j!=l)){
if(matrix[i][j] == matrix[k][l]){
int x = 0;
boolean flag = false;
while(strArr[x] != null){
if(null != matrix[i][j] && matrix[i][j].equals(strArr[x])){
flag = true;
}
x++;
}
if(flag==false){
strArr[count] = matrix[i][j];
count++;
}
}
}
}
}
}
}
return count;
}
}
6
Note : 1, 2, 3, 6, null are duplicate in matrix, hence count is 6