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
Test if all elements are present in list-Python
The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.U
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 - 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
Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical using Python. Examples: Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueCheck if all elements in a list are identical or not using a loop Start a Python for loop and check if the f
5 min read
Search Elements in a Matrix - Python
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
3 min read
Find Unique Elements from Tuple in Python
Tuples are immutable built-in data type in Python that can store multiple values in it. Extracting Unique Elements from a Tuple in Python can be done through two different approaches. Examples: Input: (1, 2, 13, 4, 3, 12, 5, 7, 7, 2, 2, 4)Output: (1, 2, 3,4,5,12,13)Input: ('Apple', 'Mango', 'Banana'
5 min read
Python - Test Consecutive Element Matrix
Given a Matrix, test if it is made of consecutive elements. Input : test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]] Output : True Explanation : Elements in Matrix range from 4 to 12. Input : test_list = [[4, 5, 6], [7], [8, 18, 10], [11, 12]] Output : False Explanation : Elements not consecutive
6 min read
Python - Check Similar elements in Matrix rows
Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List. Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : True Explanation : All rows have same elements.Input : test_list = [[1, 1,
8 min read