Try this, this approach is to build an index map for all the elements present in the List and setting the value to null if more than one value found for a key
List<List<Integer>> table = new ArrayList<>();
table.add(Arrays.asList(1, 2, 3));
table.add(Arrays.asList(4, 1, 5));
table.add(Arrays.asList(1, 6, 9));
System.out.println("Before " + table);
Map<Integer, List<List<Integer>>> indices = new HashMap<>();
for (int i = 0; i < table.size(); i++) {
for (int j = 0; j < table.get(i).size(); j++) {
int el = table.get(i).get(j);
if (indices.get(el) == null) {
indices.put(el, new ArrayList<>());
}
indices.get(el).add(Arrays.asList(i, j));
}
}
indices.keySet().stream()
.filter(k -> indices.get(k).size() > 1)
.flatMap(k -> indices.get(k).stream())
.forEach(li -> table.get(li.get(0)).set(li.get(1), null));
System.out.println("After " + table);
output
Before [[1, 2, 3], [4, 1, 5], [1, 6, 9]]
After [[null, 2, 3], [4, null, 5], [null, 6, 9]]
nullor remove them? A simpler way of doing this is to have aSet<List<Integer>>which will implicitly drop any duplicates. Do you want to remove duplicate rows or values?tableholds integerInteger(1), and third list also holds such an object: do you want to null both objects or only objects that follow the first "unique" one?List<Integer>computed and compared in betweenList<Integer>members of theSet? E.g., how will a set{ [1, 3], [1, 4] }define what duplicate inner list values are? (Pardon the syntax, I'm just getting started with Java, asking out of curiosity!)