In this article given a set(), the task is to write a Python program to access an element K, without performing deletion using pop().
Example:
Input : test_set = {6, 4, 2, 7, 9}, K = 7 Output : 3 Explanation : 7 occurs in 3rd index in set. Input : test_set = {6, 4, 2, 7, 9}, K = 9 Output : 4 Explanation : 9 occurs in 4th index in set.
Method #1: Using loop
The most generic method is to perform iteration using loop, and if K is found, print the element, and if required index.
Python3
# Python3 code to demonstrate working of # Accessing K element in set without deletion # Using loop # initializing set test_set = { 6 , 4 , 2 , 7 , 9 } # printing original set print ( "The original set is : " + str (test_set)) # initializing K K = 7 res = - 1 for ele in test_set: # checking for K element res + = 1 if ele = = K: break # printing result print ( "Position of K in set : " + str (res)) |
The original set is : {2, 4, 6, 7, 9} Position of K in set : 3
Method #2: Using next() + iter()
In this, container is converted to iterator and next() is used to increment the position pointer, when element is found, we break from loop.
Python3
# Python3 code to demonstrate working of # Accessing K element in set without deletion # Using next() + iter() # initializing set test_set = { 6 , 4 , 2 , 7 , 9 } # printing original set print ( "The original set is : " + str (test_set)) # initializing K K = 7 set_iter = iter (test_set) for idx in range ( len (test_set)): # incrementing position ele = next (set_iter) if ele = = K: break # printing result print ( "Position of K in set : " + str (idx)) |
The original set is : {2, 4, 6, 7, 9} Position of K in set : 3
Method #3: Using index()
The approach of the code is to convert the input set into a list, find the index of the given element in the list, and return the index.
Algorithm:
1. Convert the input set to a list.
2. Find the index of the given element in the list using the index() method.
3. Return the index of the given element in the original set.
Python3
def find_index(test_set, K): # convert the set to a list test_list = list (test_set) # find the index of K in the list index = test_list.index(K) # return the index of K in the original set return index # example usage test_set = { 6 , 4 , 2 , 7 , 9 } K = 7 index = find_index(test_set, K) print (index) |
3
Time complexity: O(n), where n is the number of elements in the set, because we have to convert the set to a list, which requires iterating over all the elements in the set. Additionally, finding the index of the element in the list requires a linear search of the list, which has a worst-case time complexity of O(n).
Auxiliary Space: O(n), because we create a new list that contains all the elements of the input set. The space complexity of the index() method is constant, so it does not contribute to the space complexity of the overall algorithm.
Method #4: Use a dictionary to store the index of each element in the set.
Step-by-step approach:
- Create an empty dictionary called index_dict.
- Iterate through the elements of the set using a for loop.
- For each element in the set, add a key-value pair to index_dict where the key is the element itself and the value is its index in the list (which is the length of the dictionary at that point).
- Once index_dict is fully populated, return the value of index_dict[K].
Python3
def find_index(test_set, K): # create an empty dictionary to store the index of each element in the set index_dict = {} # iterate through the elements of the set for i, elem in enumerate (test_set): # add a key-value pair to the dictionary where the key is the element and the value is its index index_dict[elem] = i # return the index of K in the original set return index_dict[K] # example usage test_set = { 6 , 4 , 2 , 7 , 9 } K = 7 index = find_index(test_set, K) print (index) |
3
Time complexity: O(n), where n is the number of elements in the set.
Auxiliary space: O(n), to store the dictionary.