This Python program reads a 4×4 matrix and checks whether any element appears more than once.
If a duplicate is found, it prints "Yes"; otherwise, it prints "No".
def read_matrix(rows: int, cols: int) -> list[list[int]]:
print(f"Enter elements for a {rows}×{cols} matrix:")
matrix = []
for i in range(rows):
row = list(map(int, input(f"Row {i+1}: ").strip().split()))
if len(row) != cols:
print("Each row must contain exactly the number of columns.")
exit()
matrix.append(row)
return matrix
def has_duplicate(matrix: list[list[int]]) -> str:
seen = set()
for row in matrix:
for val in row:
if val in seen:
return "Yes"
seen.add(val)
return "No"
# Read and check 4×4 matrix
mat = read_matrix(4, 4)
result = has_duplicate(mat)
print(f"\nIs there a duplicate? {result}")
Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 1
Output:
Is there a duplicate? Yes