What is the code to remove the repeated words from a string in Java without using the split function?
Answers
Hlew,
Here's the answer...
We can solve this problem quickly using python Counter() method. Approach is very simple.
1) Split input sentence separated by space into words.
2) So to get all those strings together first we will join each string in given list of strings.
3) Now create a dictionary using Counter method having strings as keys and their frequencies as values.
4) Join each words are unique to form single string.
from collections import Counter
def remov_duplicates(input):
# split input string separated by space
input = input.split(" ")
# joins two adjacent elements in iterable way
for i in range(0, len(input)):
input[i] = "".join(input[i])
# now create dictionary using counter method
# which will have strings as key and their
# frequencies as value
UniqW = Counter(input)
# joins two adjacent elements in iterable way
s = " ".join(UniqW.keys())
print (s)
# Driver program
if __name__ == "__main__":
input = 'Python is great and Java is also great'
remov_duplicates(input)
Thanks.
Sorry baby 'wink'