-4

Here is my code i've been stuck on, Anyone point out what's wrong here or suggest me an alternative way to achive this.

def remove_duplicates(string):

    s = string.split()
    return string.replace(s[0],"")

def RemoveDupliChar(Word):
        NewWord = " "
        index = 0
        for char in Word:
                if char != NewWord[index]:
                        NewWord += char
                        index += 1
        print(NewWord.strip()) 
Arun D
  • 444
  • 3
  • 7
  • 23
  • 3
    Possible duplicate of [Removing duplicate characters from a string](http://stackoverflow.com/questions/9841303/removing-duplicate-characters-from-a-string) – Chris_Rands Mar 17 '17 at 09:03
  • Seen that already but still same error message. – beautiful mind Mar 17 '17 at 09:09
  • What is this error message? You have dumped your code without any details. The top answer of the suggested duplicate definitely works if I understand your aim correctly – Chris_Rands Mar 17 '17 at 09:14
  • The function should remove all repeated characters in the string and return a tuple with two values: A new string with only unique, sorted characters. The total number of duplicates dropped. – beautiful mind Mar 17 '17 at 09:19

3 Answers3

0

Please try to add atleast an example if possible. It helps in understanding the problem.

Here is a possible example for your solution, (from what i understand from your question.)

>>> def func1(str1):
...     strarr1 = str1.split()
...     tempstr = []
...     for i in strarr1:
...             if i not in tempstr:
...                     tempstr.append(i);
...     print (tempstr)
...

Sample: Input: func1("he he he hello bolo bo bolo hee hee he")

output: ['he', 'hello', 'bolo', 'bo', 'hee']

Or maybe this is what you want:

>>> def func2(str1):
...     str2arr=[]
...     str3arr=[]
...     str4 = ''
...     for i in str1:
...             if i not in str2arr:
...                     str2arr.append(i)
...             else:
...                     str3arr.append(i)
...     print (str2arr)
...

Sample Input: func2("heeelo")

Output: ['h', 'e', 'l', 'o']

  • The function should remove all repeated characters in the string and return a tuple with two values: A new string with only unique, sorted characters. The total number of duplicates dropped. For example: remove_duplicates('aaabbbac') => ('abc', 5) remove_duplicates('a') => ('a', 0) remove_duplicates('thelexash') => ('aehlstx', 2) – beautiful mind Mar 17 '17 at 10:26
0

I think this may help.

def removeDupWords(list):
    newList = []
    for x in list:
        if x not in newList:
            newList.append(x)
    return newList

s="Hello World World Hello"
output=' '.join(removeDupWords(s.split(' ')))
print(output)

Output: Hello World

Manpreet Ahluwalia
  • 301
  • 1
  • 3
  • 11
  • The function should remove all repeated characters in the string and return a tuple with two values: A new string with only unique, sorted characters. The total number of duplicates dropped. For example: remove_duplicates('aaabbbac') => ('abc', 5) remove_duplicates('a') => ('a', 0) remove_duplicates('thelexash') => ('aehlstx', 2) – beautiful mind Mar 17 '17 at 10:26
0
def removeDupWords(list):
    newList = []
    tuple = []
    duplicates = 0
    for x in list:
        if x not in newList:
            newList.append(x)
        else:
            duplicates += 1
    uniqueString = ''.join(sorted(newList))
    tuple.append(uniqueString)
    tuple.append(duplicates)
    return tuple

s="hellohowareyou"
output=removeDupWords(s)
print(output)

OUTPUT:

['aehloruwy', 5]

Manpreet Ahluwalia
  • 301
  • 1
  • 3
  • 11