Given a String list, check if all Kth index elements are unique.
Input : test_list = [“gfg”, “best”, “for”, “neveropen”], K = 1
Output : False
Explanation : e occurs as 1st index in both best and neveropen.
Input : test_list = [“gfg”, “best”, “neveropen”], K = 2
Output : True
Explanation : g, s, e, all are unique.
Method #1 : Using loop
This is brute way to solve this problem. In this, we iterate for each string and flag off when any element is repeated, and return false.
Python3
# Python3 code to demonstrate working of # Check if Kth index elements are unique # Using loop # initializing list test_list = [ "gfg" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 res = [] flag = True for ele in test_list: # checking if element is repeated if ele[K] in res: flag = False break else : res.append(ele[K]) # printing result print ( "Is Kth index all unique : " + str (flag)) |
The original list is : ['gfg', 'best', 'for', 'neveropen'] Is Kth index all unique : True
Method #2 : Using Counter() + all()
In this, we count frequency of each char. at Kth index, and all() is used to check if Counter is less than 2 for all.
Python3
# Python3 code to demonstrate working of # Check if Kth index elements are unique # Using Counter() + all() from collections import Counter # initializing list test_list = [ "gfg" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 # getting count of each Kth index item count = Counter(sub[K] for sub in test_list) # extracting result res = all (val < 2 for val in count.values()) # printing result print ( "Is Kth index all unique : " + str (res)) |
The original list is : ['gfg', 'best', 'for', 'neveropen'] Is Kth index all unique : True
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using list(),set() methods
Python3
# Python3 code to demonstrate working of # Check if Kth index elements are unique # initializing list test_list = [ "gfg" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 res = False if (test_list[K] ! = list ( set (test_list[K]))): res = True # printing result print ( "Is Kth index all unique : " + str (res)) |
The original list is : ['gfg', 'best', 'for', 'neveropen'] Is Kth index all unique : True
Method #4: Using list comprehension and set()
Algorithm:
- Initialize an empty set and an empty list to keep track of unique elements.
- Traverse through the given list and extract the Kth index element of each string.
- Check if the element is already present in the set.
- If it is already present, return False as it means there is a duplicate.
- If not, add the element to the set and the list.
- After traversing the whole list, compare the length of the set and the list.
- If they are equal, it means all elements were unique. Return True.
- If not, it means there was at least one duplicate. Return False.
Python3
#Python3 code to demonstrate working of #Check if Kth index elements are unique #Using set #initializing list test_list = [ "gfg" , "best" , "for" , "neveropen" ] #printing original list print ( "The original list is : " + str (test_list)) #initializing K K = 2 #Using set to check unique Kth index elements unique = len ( set ([ele[K] for ele in test_list])) = = len ([ele[K] for ele in test_list]) #printing result print ( "Is Kth index all unique : " + str (unique)) #This code is contributed by Vinay Pinjala. |
The original list is : ['gfg', 'best', 'for', 'neveropen'] Is Kth index all unique : True
Time Complexity: O(n), where n is the number of strings in the list. We need to traverse through the entire list only once.
Space Complexity: O(n), where n is the number of strings in the list. In the worst case, all Kth index elements are unique, so the set and the list will contain n elements.
Method 5 : using dictionary.
steps to implement this method:
Initialize an empty dictionary to keep track of the frequency of each element at the Kth index.
Loop through the elements in the input list and check if the element at the Kth index is already in the dictionary.
If the element is already in the dictionary, increment its frequency by 1.
If the element is not in the dictionary, add it with frequency 1.
Finally, check if any element in the dictionary has a frequency greater than 1. If so, return False, else return True.
Python3
test_list = [ "gfg" , "best" , "for" , "neveropen" ] print ( "The original list is : " + str (test_list)) K = 2 freq_dict = {} for ele in test_list: if ele[K] in freq_dict: freq_dict[ele[K]] + = 1 else : freq_dict[ele[K]] = 1 unique = all (value = = 1 for value in freq_dict.values()) print ( "Is Kth index all unique : " + str (unique)) |
The original list is : ['gfg', 'best', 'for', 'neveropen'] Is Kth index all unique : True
The time complexity of this approach is O(N), where N is the length of the input list.
The auxiliary space required by this approach is O(K), where K is the number of unique elements at the Kth index.