How to check if a matrix rows contain numbers without repetition in Python

3 Answers

0 votes
def rows_have_unique_numbers(matrix):
    return all(len(row) == len(set(row)) for row in matrix)

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 7]  # This row has a repetition
]

print(rows_have_unique_numbers(matrix))  




'''
run:

False

'''

 



answered May 23, 2025 by avibootz
edited May 28, 2025 by avibootz
0 votes
def rows_have_unique_numbers(matrix):
    for row in matrix:
        seen = set()
        for num in row:
            if num in seen:
                return False
            seen.add(num)
            
    return True

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 7]  # This row has a repetition
]

print(rows_have_unique_numbers(matrix))  



'''
run:

False

'''

 



answered May 23, 2025 by avibootz
0 votes
import numpy as np

def rows_have_unique_numbers(matrix):
    return all(len(np.unique(row)) == len(row) for row in matrix)

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 7]  # This row has a repetition
])

print(rows_have_unique_numbers(matrix)) 



'''
run:

False

'''

 



answered May 23, 2025 by avibootz
edited May 24, 2025 by avibootz
...