Find Unique Elements from Tuple Using Enumeration()
Python
# function for finding unique values def unique_numbers(numbers): un = tuple (j for i, j in enumerate (numbers) if numbers.index(j) = = i) return un # print the unique tuple by extracting all the unique elements numbers = ( 1 , 2 , 3 , 4 , 2 , 2 , 2 , 1 , 5 , 4 , 3 , 4 , 3 ) print (unique_numbers(numbers)) |
Output:
(1, 2, 3, 4, 5)
Time Complexity: O(N) Where N is the length of the Test tuple.
Auxiliary Space: O(M) Where M is the length of the new Tuple.
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', 'Mango', 'Apple')
Output: ('Apple', 'Mango', 'Banana')
Let’s start with the different methods :