Remove repeated words from a sentence
Coding problemEasy-MediumAsked in: Infosys
Given a sentence, remove every repeated word: keep the first occurrence of each word, as it is written, and drop the later ones. Return the remaining words joined by single spaces.
Words are separated by one or more spaces, and the sentence may start or end with spaces. Compare words without regard to case: in "Test the test", the second "test" is a repeat of "Test".
Examples
Example 1
Input: sentence = "this is a test this is only a test"
Output: "this is a test only"
Example 2
Input: sentence = "Test the test"
Output: "Test the"
Example 3
Input: sentence = " "
Output: ""
Constraints
- The length of sentence is between 0 and 100,000
- Words hold English letters and digits; only the space character separates them
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.