[SOLVED] How to detect any duplicate in each row and column in 2d array in python

Issue

This Content is from Stack Overflow. Question asked by Trying_To_be_a_coder

I was given an assignment to check for duplicates. If there are duplicates then we have to return false else true. As I am new to python I am unable to write the code. So it will be helpful if you guys help me with it.
For Example

    test = ([1,2,3,4],
    [2,3,1,4],
    [1,2,3,4])

should return false.



Solution

You have to iterate through each element and perform two checks – the row and a column – if duplicate occurs return false. Hence the array is two-dimensional you will have to perform a nested loop:

    for row in array:
        for cell in row:
            if check(array[row][cell]):
                return false
    return true

The check() function have to test, if any other cell in the same column or row is the same. So simmilar iteration is to do. Remember not to check the cell with its own value. Details of implementation are great excercise, try to do it by your own.


This Question was asked in StackOverflow by Trying_To_be_a_coder and Answered by wojciech It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?