I have a list of indexes A :
A = np.array([[1, 4, 3], [1, 2, 5], [6, 7, 8], [9, 10, 2], [11, 3, 12]])
I would like to delete each lines that contains at least one duplicated value (no matter in which column it’s located) to obtain an array with no duplicates indexes :
[[1, 4, 3], [6, 7, 8], [9, 10, 2],
Is there a quick and convenient way to perform this ?
Given that no sub-list should be in the output if a value in it has already been in any previous sub-list then one possible solution can look like this,
# Assuming that A is a 2-D list output =  # Add all values to a set st = set() for i in range(len(A)): for j in range(len(A[i])): if A[i][j] not in st: st.add(A[i][j]) for i in range(len(A)): flag = True j = 0 while j < len(A[i]): if A[i][j] not in st: # set flag to false if value not in set meaning a duplicate is encountered flag = False break else: # if a value is in the set then remove it from set st.remove(A[i][j]) j += 1 # append A[i] to output only if the sub-list did not encounter any duplicates if flag: output.append(A[i])
The time complexity will be O(n2).
When the input is,
A = [[1, 4, 3], [1, 2, 5], [6, 7, 8], [9, 10, 2], [11, 3, 12]]
It provides the output,
[[1, 4, 3], [6, 7, 8], [9, 10, 2]]