1

I count with the following example array

let animals = ['dog', 'cat', 'egypt cat', 'fish', 'golden fish'] 

the basic idea is get to the following result removing the elements which are included on the other strings

['dog', 'egypt cat', 'golden fish'] 

My approach was detect which are included iterating twice on the array and comparing the values

let arr2 = []
arr.forEach((el, i) => {
    arr.forEach((sub_el, z) => {
        if (i != z && sub_el.includes(el)) {
          arr2.push(el)
        }
      })
    })

then filter the array with those matched values. Anyone has a simplest solution?

Joaquin Diaz
  • 184
  • 1
  • 5
  • 16
  • Does this answer your question? [Remove occurrences of duplicate words in a string](https://stackoverflow.com/questions/16843991/remove-occurrences-of-duplicate-words-in-a-string) – Uzair Sep 28 '20 at 14:36
  • For your `animals` example input, do you want to remove *all* values from it? – CertainPerformance Sep 28 '20 at 14:38

1 Answers1

1

You need to itterate the array again and then check any string.

This approach minimizes the iteration by a short circuit on found of a matching string.

let animals = ['dog', 'cat', 'egypt cat', 'fish', 'golden fish'],
    result = animals.filter((s, i, a) => !a.some((t, j) => i !== j && t.includes(s)));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392