Remove Duplicate Words

Is it possible to have a Remove Duplicate Words function?

thank popclip, always.

1 Like

That’s an interesting one, can you give an example of what it would do exactly?

1 Like

thanks for replied,
just like this site: Remove Duplicate Words and Repeated Keywords (tracemyip.org)

Use this RegEx with replace $1
\b(\w+)(?:\W+\1\b)+

Good bye bye world world

Becomes
Good bye world

1 Like

Here’s that as a snippet:

#popclip - remove duplicated words, attempt 1
name: Remove Duplicates
icon: square dup
after: paste-result
javascript: |
  const regex = /\b(\w+)(?:\W+\1\b)+/g 
  return popclip.input.text.replace(regex, (_, word) => word)

(The above block is an extension snippet - select it to install the extension with PopClip)

Pretty good but it only removes duplicates that are adjacent, so: one one two twoone two,
but: one two two oneone two one :thinking:

Let me try another approach:

#popclip - remove duplicated words attempt 2
name: Remove Duplicates
icon: square dup
after: paste-result
javascript: |
  const words = popclip.input.text.split(/\s/)
  return [...new Set(words)].join(' ')
  // technique thanks to <https://stackoverflow.com/a/14438954/220847>
  // also note: "iteration of sets visits items in insertion order" (MDN)

This time: one two two oneone two :trumpet::tada:

1 Like

you guys are geniuses :hot_face:

1 Like