Open In App

Python – Test if all elements are unique in columns in a Matrix

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Matrix, test if all columns contain unique elements.

Input : test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]] 
Output : True 
Explanation : 3, 1, 4; 4, 2, 1; 5, 4, 10; All elements are unique in columns.

Input : test_list = [[3, 4, 5], [3, 2, 4], [4, 1, 10]] 
Output : False 
Explanation : 3, 3, 4; 3 repeated twice. 
 

Method #1 : Using loop + set() + len()

In this, we iterate for each column and test for unique elements using set size using len(), if any column is found having a size not equal to the actual list, then the result is flagged off.

Python3




# Python3 code to demonstrate working of
# Test if all elements Unique in Matrix Columns
# Using loop + set() + len()
 
# initializing list
test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
 
# printing original lists
print("The original list is : " + str(test_list))
 
res = True
for idx in range(len(test_list[0])):
     
    # getting column
    col = [ele[idx] for ele in test_list]
     
    # checking for all Unique elements
    if len(list(set(col))) != len(col):
        res = False
        break
 
# printing result
print("Are all columns Unique : " + str(res))


Output:

The original list is : [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
Are all columns Unique : True

Time Complexity: O(n*m)
Auxiliary Space: O(k)

Method #2 : Using all() + list comprehension

This can be solved in one-liner using all() which checks for all the columns,  made using list comprehension, if all columns return True, all() returns true.

Python3




# Python3 code to demonstrate working of
# Test if all elements Unique in Matrix Columns
# Using loop + set() + len()
 
# initializing list
test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
 
# printing original lists
print("The original list is : " + str(test_list))
 
res = True
for idx in range(len(test_list[0])):
     
    # getting column
    col = [ele[idx] for ele in test_list]
     
    # checking for all Unique elements
    if len(list(set(col))) != len(col):
        res = False
        break
 
# printing result
print("Are all columns Unique : " + str(res))


Output:

The original list is : [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
Are all columns Unique : True

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  The time complexity of the all() and list comprehension is O(n)
Auxiliary Space: O(1), no extra space is required

Method #3 : Using loop + Counter() function

Python3




# Python3 code to demonstrate working of
# Test if all elements Unique in Matrix Columns
# Using loop + Counter() function
from collections import Counter
# initializing list
test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
 
# printing original lists
print("The original list is : " + str(test_list))
 
res = True
for idx in range(len(test_list[0])):
 
    # getting column
    col = []
    for ele in test_list:
        col.append(ele[idx])
    freq = Counter(col)
    # checking for all Unique elements
    if(len(freq.keys()) != len(col)):
        res = False
        break
 
# printing result
print("Are all columns Unique : " + str(res))


Output

The original list is : [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
Are all columns Unique : True

Time Complexity: O(N*M) 
Auxiliary Space: O(N*M)

Method #4: Using numpy:

Algorithm:

  1. Initialize the list to check for uniqueness in columns.
  2. For each column in the transposed matrix of the input list, check if the number of unique elements is the same as the length of the column.
  3. If any column is found to have duplicate elements, set the result to False and break the loop.
  4. If all columns have unique elements, set the result to True.
  5. Print the result.

Python3




# importing numpy
import numpy as np
# initializing list
test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
# printing original lists
print("The original list is : " + str(test_list))
# checking for all unique columns using numpy
res = all(len(np.unique(col)) == len(col) for col in zip(*test_list))
# printing result
print("Are all columns Unique : " + str(res))
#This code is contributed by Rayudu


Output:

The original list is : [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
Are all columns Unique : True

Time complexity: O(nm log m)
where n is the number of rows and m is the number of columns in the input matrix. This is because we are iterating over each column of the transposed matrix and checking if the length of the unique elements in each column is equal to the length of the column. The time complexity of np.unique() function is O(m log m) where m is the number of elements in the array.

Auxiliary Space: O(m)
where m is the number of columns in the input matrix. This is because we are storing each column temporarily in the list comprehension before checking for uniqueness. Additionally, the space complexity of the np.unique() function is O(m).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads