Saturday, October 25, 2025
HomeLanguagesPython | Remove unordered duplicate elements from a list

Python | Remove unordered duplicate elements from a list

Given a list, the task is to remove the duplicate elements. All the elements which are not in same order but made of same characters/numbers are considered as duplicates. 

Examples: 

Input : ['gfg', 'ggf', 'fgg', 'for', 'orf',
                'ofr', 'rfo', 'rof', 'fro']
Output : ['for', 'fgg']

Input:  ['110', '101', '001', '010', '100']
Output:  ['001', '011']

  Method #1 : Using set 

Python3




# Python code to remove duplicate
# unordered elements from a list
from collections import Counter
 
# List initialisation
Input = ['1213','1231','1123','1132','2113',
         '2311','0007', '0016', '0025', '0034',
         '0043', '0052', '0061', '0070','0304',
         '0313', '0322','0098','9800', '0331',
         '0340', '0403', '0412', '0421', '0430',
         '0502','8900','8009' ,'0511', '0520',
         '0601', '0610', '0700', '1006', '1015']
 
# Set initialisation
s = set()
 
# Output list initialisation
output =[]
 
for i in Input:
    if tuple(Counter(sorted(i, key = int)).items()) in s:
        pass
         
    else:
        s.add(tuple(Counter(sorted(i, key = int)).items()))
        output.append(i)
 
# Printing output
print(output)


Output:

['1213', '0007', '0016', '0025', '0034', 
 '0313', '0322', '0098', '0412', '0511']

Time Complexity: O(n), where n is the length of the list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

  Method #2 : 

Python3




# Python code to remove duplicate
# unordered elements from a list
# List initialisation
Input = ['gfg', 'ggf', 'fgg', 'for', 'orf',
                'ofr', 'rfo', 'rof', 'fro']
 
# Getting unique nos
Output = list({''.join(sorted(n)) for n in Input})
 
# Printing Output
print(Output)


Output:

['for', 'fgg']

Method #3:Using Counter() method

Python3




# Python code to remove duplicate
# unordered elements from a list
from collections import Counter
# List initialisation
Input = ['gfg', 'ggf', 'fgg', 'for', 'orf',
                'ofr', 'rfo', 'rof', 'fro']
 
sortedList = list(''.join(sorted(n)) for n in Input)
 
freq = Counter(sortedList)
output = list(freq.keys())
output.sort()
# Printing Output
print(output)


Output

['fgg', 'for']

Time Complexity:O(NLogN)

Auxiliary Space: O(N)

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

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS