Created
July 13, 2018 12:45
-
-
Save mykhailokrainik/ab43488811d4fbe9973f264b814c8131 to your computer and use it in GitHub Desktop.
Remove duplicate words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Your task is to remove all duplicate words from string, leaving only single words entries. | |
Example: | |
Input: | |
'alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta' | |
Output: | |
'alpha beta gamma delta' | |
=end | |
def remove_duplicate_words(s) | |
s.split(" ").reduce([]){|acc, c| acc.include?(c) ? nil : acc << c; acc }.join(" ") | |
end | |
describe "Remove Duplicate Words" do | |
Test.assert_equals(remove_duplicate_words("alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta"), "alpha beta gamma delta") | |
Test.assert_equals(remove_duplicate_words("my cat is my cat fat"), "my cat is fat") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment