Anton Sokolovskii
Anton Sokolovskii

Reputation: 29

Remove duplicate words

I have a string with a few words:

$list1= "apple tree dog cat apple horse tree"

How I can delete duplicate word?

Upvotes: 0

Views: 1240

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You first have to split the string to receive a list of words. Using Select -Unique will remove all duplicates and finally to transform the list into a single string, you join them using -join:

($list1 -split ' ' | Select -Unique) -join ' '

Output:

apple tree dog cat horse

Upvotes: 6

Related Questions