Saturday, October 5, 2024
Google search engine
HomeLanguagesPython Program to find XOR of all the key value pairs in...

Python Program to find XOR of all the key value pairs in a Dictionary

Given a dictionary in python, write a program to find the XOR of all the key-value pairs in the dictionary and return in the form of an array.

Note: All the keys and values in the dictionary are integers.

Examples:

Input : dic={1:3, 4:5, 6:7, 3 :8}
Output : [2, 1, 1, 11]
Explanation: XOR of all the key-value pairs in the dictionary are [1^3, 4^5, 6^7, 3^8] 
Thus, [2, 1, 1, 11] 

Note: Order may change as the dictionary is unordered.

Method 1:

  • Start traversing the dictionary.
  • Maintain an array for storing the XOR of each key-value pair.
  • For each key in the dictionary, find  key^value and append the value to the array.
  • Finally, return the array.

Below is the implementation of the above approach

Python3




def xorOfDictionary(dic):
    # Array to store XOR values
    arr = []
 
    # Traversing the dictionary
    for i in dic:
        # Finding XOR of Key value.
        cur = i ^ dic[i]
        arr.append(cur)
    return arr
 
 
dic = {5: 8, 10: 9, 11: 12, 1: 14}
print(xorOfDictionary(dic))


Output

[13, 3, 7, 15]

Time complexity: O(n)
Auxiliary space : O(n) for storing XOR values.

Method  2:Using items() function

Python3




def xorOfDictionary(dic):
    # Array to store XOR values
    arr = []
 
    # Traversing the dictionary
    for key, value in dic.items():
        # Finding XOR of Key value.
        cur = key ^ value
        arr.append(cur)
    return arr
 
 
dic = {5: 8, 10: 9, 11: 12, 1: 14}
print(xorOfDictionary(dic))


Output

[13, 3, 7, 15]

Time complexity: where n is the number of items in the dictionary, because it has to traverse the dictionary once to calculate the XOR of each key-value pair.
Auxiliary space: O(n), because it needs to store all the XOR values in an array.

Method #3 : Using keys() and values() methods

Python3




dic = {5: 8, 10: 9, 11: 12, 1: 14}
arr = []
x=list(dic.keys())
y=list(dic.values())
for i in range(0,len(x)):
    arr.append(x[i]^y[i])
print(arr)


Output

[13, 3, 7, 15]

Time complexity: O(n)
Auxiliary space: O(n) for storing XOR values.

Method 4: Using a list comprehension

Python3




def xorOfDictionary(dic):
  # List comprehension to compute XOR values
  return [i ^ dic[i] for i in dic]
 
dic = {5: 8, 10: 9, 11: 12, 1: 14}
print(xorOfDictionary(dic))


Output

[13, 3, 7, 15]

Time complexity: O(n)
Auxiliary space: O(n) 

Method 5:Using the map() function and a lambda function

  • Traverse the dictionary using the .items() method.
  • For each key-value pair, apply the lambda function, which takes the XOR of the key and value.
  • The result of each XOR operation is stored in a list using the map() function.
  • Return the list of XOR values.

Code uses the map() function with a lambda function to perform the XOR operation on each key-value pair in the dictionary and store the result in a list.

Python3




dict = {5: 8, 10: 9, 11: 12, 1: 14}
 
xor_res = list(map(lambda kv: kv[0] ^ kv[1], dict.items()))
 
# Result
print(xor_res)


Output

[13, 3, 7, 15]

The items() method is used by the map() function to extract the key-value pairs for each item in the dictionary. The list() method is used to turn iterable that the map() function returns into a list. The list of XOR results is printed last.

Time complexity: O(N) as the map() function and the lambda function are both applied to each key-value pair once, which takes O(1) time and there are a total of N key-value pairs. So time complexity is O(N).
Auxiliary space: O(n) as the list is of the same size as the number of key-value pairs in the dictionary. Therefore, the space complexity is O(N).

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
You’ll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We’re here to prepare you thoroughly for online assessments and interviews.
Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000 neveropen! Whether it’s DSA in C++, Java, Python, or JavaScript we’ve got you covered. Let’s embark on this exciting journey together!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments