Reputation: 31
I was trying to compare the values of each element in a row of a 2D matrix to make sure every element is different from each other.
Here's my function:
private static boolean CompareComponentValue(int[][] m) {
int k = 0;
for (int i = 0; i < m.length; i = i + 1) {
for (int j = 1; j < m[i].length; j = j + 1) {
if (m[i][k] == m[i][j]) {
return false;
}
}
k = k + 1;
}
return true;
}
I was thinking that I had to campare each element like this: [0][0] is different from [0][1], [0][2] ...[0][n], then: [0][1] is different from [0][2]...[0][n] and also for each row of course, [1][0] is different from [1][1], [1][2]...[1][n].
I can't get it to work properly because it will return false sometimes when it shouldn't.
Upvotes: 0
Views: 530
Reputation: 79580
A simple way is to create a Set
out of each row and compare its size with the size of the row. A Set
automatically discards duplicate values and therefore if the size of the Set
is equal to the size of the row, all the elements in the row are unique.
Demo:
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public class Solution {
public static void main(String[] args) throws IOException {
Integer n[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Integer x[][] = { { 1, 2, 2 }, { 4, 5, 6 }, { 7, 8, 9 } };
System.out.println(compareComponentValue(n));
System.out.println(compareComponentValue(x));
}
private static boolean compareComponentValue(Integer[][] m) {
for (int i = 0; i < m.length; i++) {
// Add the row i.e. m[i] to a Set
Set<Integer> set = Arrays.stream(m[i]).collect(Collectors.toSet());
// If there will be any duplicate values in m[i], they will be rejected by the
// set and therefore the size of the set won't be equal to the size of m[i]
if (set.size() != m[i].length)
return false;
}
return true;
}
}
Output:
true
false
Upvotes: 1
Reputation: 31
First of all thank you guys for your answers but they really went above my level, as I've stated at the beggining I'm not using any packages for now because I'm still a beeginer and I want to stay with basic logic and in the end I got an answer by thinking hard.
private static boolean NumerosDistintosFila(int[][] m) {
for (int i = 0; i < m.length; i = i + 1) {
for (int k = 0; k < m.length - 2; k = k + 1) {
for (int j = 1 + k; j < m[i].length; j = j + 1) {
if (m[i][k] == m[i][j]) {
return false;
}
}
}
}
return true;
}
I added another for loop with a variable k which I noticed didn't ever go above the matrix's lenght - 2, for example if I were to use a 5x5 I'll need it to go as far as 3. This is because the last element I need to compare in the first row for example is [0][3] to [0][4].
My logic was that I needed to compare elements without repeating the comparations between them so what this code does is basically compare the first element with every element in the same row, then compare the second element with every element except the first one then the third element with the fourth and the fifth etc..
Again thank you guys for your solutions I tried them out and all of them worked perfectly but as I said I want to stay with basic logic for now.
Upvotes: 1
Reputation: 51152
"... to make sure every element is different from each other."
What you need is a Set. Adding an element to a set, and testing whether an element is in the set already, are easy to do using the add
and contains
operations. Also, these operations both take O(1) time, so the solution will be efficient; no need to search the matrix again and again.
Also, use the "enhanced for
loop" if you just need the values, not the indices.
import java.util.*;
public class CheckMatrix {
public static boolean hasDuplicate(int[][] matrix) {
Set<Integer> seen = new HashSet<>();
for(int[] row : matrix) {
// empty the set, to only check for duplicates within a single row
seen.clear();
for(int x : row) {
if(seen.contains(x)) { return true; }
seen.add(x);
}
}
return false;
}
}
If you want to be fancy, you can use the fact that the add method returns a boolean
indicating whether x
was not already present: replace the inner loop body with:
if(!seen.add(x)) { return true; }
Upvotes: 0