Python - Test if all elements are unique in columns in a Matrix
Last Updated :
27 Mar, 2023
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))
OutputThe 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:
- Initialize the list to check for uniqueness in columns.
- 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.
- If any column is found to have duplicate elements, set the result to False and break the loop.
- If all columns have unique elements, set the result to True.
- 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).
Similar Reads
Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read
Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read
Python - Test if all elements in list are of same type When working with lists in Python, there are times when we need to ensure that all the elements in the list are of the same type or not. This is particularly useful in tasks like data analysis where uniformity is required for calculations. In this article, we will check several methods to perform th
2 min read
Python - Test if all elements in list are of same type When working with lists in Python, there are times when we need to ensure that all the elements in the list are of the same type or not. This is particularly useful in tasks like data analysis where uniformity is required for calculations. In this article, we will check several methods to perform th
2 min read
Python - Check if Kth index elements are unique Given a String list, check if all Kth index elements are unique. Input : test_list = ["gfg", "best", "for", "geeks"], K = 1 Output : False Explanation : e occurs as 1st index in both best and geeks.Input : test_list = ["gfg", "best", "geeks"], K = 2 Output : True Explanation : g, s, e, all are uniqu
5 min read
Python - Test if all rows contain any common element with other Matrix Given two Matrices, check if all rows contain at least one element common with the same index row of other Matrix. Input : test_list1 = [[5, 6, 1], [2, 4], [9, 3, 5]], test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 10]] Output : True Explanation : 1, 2 and 3 are common elements in rows. Input : test_lis
5 min read