Tripadvisor interview question

Write a function to remove repeated spaces from a string (ex: "one two three four" -> "one two three four")

Interview Answers

Anonymous

19 Dec 2014

Just use 2 pointers to walk through and modify the string in place.

Anonymous

9 Apr 2015

If my understanding is correct. Just "xxx".split("\\s+") and put them back together. Took within 10 lines of code.

Anonymous

9 Apr 2015

I forgot to mention that do trim() first before split.

Anonymous

4 Mar 2016

Javascript version: ' This is a test '.trim().replace(/\ {2,}/g, ' ')