Sometimes, we just wish to work with unique elements and any type of repetition is not desired, for these cases, we need to have techniques to solve these problems. One such problem can be to test for unique keys and values. For keys, they are by default unique, hence no external testing is required, but as for values, we need to have ways to do it. Let’s test various ways in which this can be done.
Method #1: Using loops In the Naive method to perform this particular task, we can check for each value and insert each value in list/hash in dictionary and when i’l repetition occurs, just stop the flow and return false.
Python3
test_dict = { 'Manjeet' : 1 , 'Akash' : 2 , 'Akshat' : 3 , 'Nikhil' : 1 }
print ( "The original dictionary : " + str (test_dict))
flag = False
hash_val = dict ()
for keys in test_dict:
if test_dict[keys] in hash_val:
flag = True
break
else :
hash_val[test_dict[keys]] = 1
print ( "Does dictionary contain repetition : " + str (flag))
|
OutputThe original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True
Time Complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary Space: O(n), where n is the number of elements in the dictionary, as we are storing all the values in the hash_val dictionary.
Method #2 :Using len() + set() + values() This problem can be easily solved using the combination of above three functions. The set function can be used to convert the values to set, removing duplicates and values function can be used to access the values.
Python3
test_dict = { 'Manjeet' : 1 , 'Akash' : 2 , 'Akshat' : 3 , 'Nikhil' : 1 }
print ( "The original dictionary : " + str (test_dict))
flag = len (test_dict) ! = len ( set (test_dict.values()))
print ( "Does dictionary contain repetition : " + str (flag))
|
OutputThe original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using values() and count() method
Python3
test_dict = { 'Manjeet' : 1 , 'Akash' : 2 , 'Akshat' : 3 , 'Nikhil' : 1 }
print ( "The original dictionary : " + str (test_dict))
flag = False
x = list (test_dict.values())
for i in x:
if x.count(i) > 1 :
flag = True
break
print ( "Does dictionary contain repetition : " + str (flag))
|
OutputThe original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using Counter() function
Python3
from collections import Counter
test_dict = { 'Manjeet' : 1 , 'Akash' : 2 , 'Akshat' : 3 , 'Nikhil' : 1 }
print ( "The original dictionary : " + str (test_dict))
flag = True
freqKeys = Counter( list (test_dict.keys()))
freqValues = Counter( list (test_dict.values()))
if (freqKeys = = len (test_dict) and freqValues = = len (test_dict)):
flag = False
print ( "Does dictionary contain repetition : " + str (flag))
|
OutputThe original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5: using operator.countOf() method
Python3
import operator as op
test_dict = { 'Manjeet' : 1 , 'Akash' : 2 , 'Akshat' : 3 , 'Nikhil' : 1 }
print ( "The original dictionary : " + str (test_dict))
flag = False
x = list (test_dict.values())
for i in x:
if op.countOf(x, i) > 1 :
flag = True
break
print ( "Does dictionary contain repetition : " + str (flag))
|
OutputThe original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True
Time Complexity: O(N)
Auxiliary Space : O(1)
Method 6: Using set() function
In this method, we create a set of the values of the dictionary using the set() function, which only contains unique elements. We compare the length of this set with the length of the original dictionary. If they are equal, it means that all the values in the dictionary are unique.
Python3
test_dict = { 'Manjeet' : 1 , 'Akash' : 2 , 'Akshat' : 3 , 'Nikhil' : 1 }
print ( "The original dictionary : " + str (test_dict))
flag = len (test_dict) = = len ( set (test_dict.values()))
print ( "Does dictionary contain repetition : " + str ( not flag))
|
OutputThe original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True
The time complexity of the given code is O(n), where n is the number of key-value pairs in the dictionary.
The auxiliary space used by the code is O(n), where n is the number of key-value pairs in the dictionary.
Method 7: Using the lambda function and map():
Algorithm:
- Initialize a dictionary test_dict with key-value pairs.
- Using lambda function and map(), extract all the values from the dictionary and convert them into a list.
- Use set() to extract unique values from the list.
- Check if the length of the original list and the set are equal or not. If they are not equal, then the dictionary contains repetitions.
- Print the result.
Python3
test_dict = { 'Manjeet' : 1 , 'Akash' : 2 , 'Akshat' : 3 , 'Nikhil' : 1 }
print ( "The original dictionary : " + str (test_dict))
flag = len ( list ( map ( lambda x: x, test_dict.values()))) ! = len ( set (test_dict.values()))
print ( "Does dictionary contain repetition : " + str (flag))
|
OutputThe original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True
Time complexity: O(n), where n is the number of values in the dictionary.
Space complexity: O(n), where n is the number of values in the dictionary (for the list created using map() function).has context menu