Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicate words except numbers

I need to remove duplicate words in a string, but not the numeric characters.

Here’s an example of text that I need to convert and keep the numeric characters.
Any suggestions on how to handle this?

String example: "Rim – Rim Black 28H 700mm x 700mm"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  static removeDuplicateWords = (statement: string) => {
    return statement
      .split(" ")
      .filter((item, pos, self) => {
        return self.indexOf(item) === pos;
      })
      .join(" ");
  };

Current Results: – Rim Black 28H x 700mm

Expected results: – Rim Black 28H 700mm x 700mm

Thanks for the help

>Solution :

Use a regular expression to test if the word contains a digit.

static removeDuplicateWords = (statement: string) => {
  return statement
    .split(" ")
    .filter((item, pos, self) => item.match(/\d/) || self.indexOf(item) === pos)
    .join(" ");
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading