in reply to remove duplicates

Could you be a bit more specific? From your example, it's clear you want to remove duplicate "word"s, but to me it's not clear which whitespace should be kept. There's a newline between bar and foo, and you want to keep the newline, even while you want the bar and foo removed. (All code given so far in this thread fail to do so).

What are your requirements for whitespace retention? A single example is just too vague.

Abigail