def remove_duplicate(string):
new = ""
for i in string:
if i not in new and string.count(i) >= 1:
new += i
return new
sample input "AAAAABBBBSSSSS"
sample output "ABS"
sample input [1,2,4,3,2,2,2]
sample output [1,2,4,3]
sample input {"hello": 3, "hi": 1 , "bye" : 2}
sample output {"hello": 1, "hi": 1 , "bye" : 1}
sample input (1,2,3,3,4,4)
sample output (1,2,3,4)
only able to solve for string and lists, above code only works for strings not able to solve for dictionaries and all data types together
This is exactly what you want, the other answer is not useful about strings and dicts, also conversion to main type is skipped:
def remove_duplicates(input_argument):
input_type = type(input_argument)
if input_type is str:
result = ''
for character in input_argument:
if character not in result:
result += character
elif input_type is dict:
result = {}
for key in input_argument:
result[key] = 1 # or any other value you want
else:
result = input_type(set(input_argument))
return result
Now your examples:
remove_duplicates("AAAAABBBBSSSSS")
# "ABS"
remove_duplicates([1,2,4,3,2,2,2])
# [1,2,4,3]
remove_duplicates({"hello": 3, "hi": 1 , "bye" : 2})
# {"hello": 1, "hi": 1 , "bye" : 1}
remove_duplicates((1,2,3,3,4,4))
# (1,2,3,4)
You can use set
to achieve the same , then convert back to the kind/type of output you want.
Something like .
def remove_duplicate(input_parameter):
unique =set(input_parameter)
# Do the type conversion or format
# of output you want
return unique
It will work for all other type but for dictionary . For dictionary it is not clear what you want to acheive.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.