In Java, using the following function for a huge matrix X to print its column-distinct elements:
// create the list of distinct values
List<Integer> values = new ArrayList<Integer>();
// X is n * m int[][] matrix
for (int j = 0, x; j < m; j++) {
values.clear();
for (int i = 0; i < n; i++) {
x = X[i][j];
if (values.contains(x)) continue;
System.out.println(x);
values.add(x);
}
}
First I iterate by columns (index j) and inside by rows (index i).
This function will be called millions of times for different matrices, so the code should be optimized to meet the performance requirements. I'm wondering about the values array. Would it be faster to use values = new ArrayList<Integer>(); or values = null instead of values.clear() ?