Home > Blockchain >  Best way to remove duplicate sentences from an array of sentences based on their characters, whitesp
Best way to remove duplicate sentences from an array of sentences based on their characters, whitesp

Time:10-16

Let say we have an array like this

const array = ['bic ycle drive', 'bici ycl frei', 'bicyc le dri ve', 'manace', 'bicycle drive', 'bicycle drive']

I want it to return ['bic ycle drive', 'bici ycl frei', 'manace'] because bic ycle drive, bicyc le dri ve, bicycle drive, bicycle drive are the same sentence with white space in different places.

Summary: Just return unique values, white space is not important.

Ps: We can pick any of the duplicates.

Thank you.

CodePudding user response:

Since ES6 it's easy to create arrays of unique values using a Set, but in this case you need to transform the strings (remove spaces) to find the duplicates, but still preserve the original strings, so you can return them. You can do this by creating a Map with the "string without space" as the key, and the original string as the value. Since Map's keys are unique.

Create a Map by using Array.map() to generate an array of [string without spaces, string]. Then convert the Map.values() iterator back to an array. This will return the last item in each series of duplicates.

const array = ['bic ycle drive', 'bici ycl frei', 'bicyc le dri ve', 'manace', 'bicycle drive', 'bicycle drive']

const result = [...new Map(
  array.map(str => [str.replace(/\s /g, ''), str])
).values()]

console.log(result)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

If you want the first item in a series of duplicates, you can use Array.reduce() to create the Map, and only assign keys if they don't alreay exist:

const array = ['bic ycle drive', 'bici ycl frei', 'bicyc le dri ve', 'manace', 'bicycle drive', 'bicycle drive']

const result = [...array.reduce((acc, str) => {
  const key = str.replace(/\s /g, '')
  
  return acc.has(key) ? acc : acc.set(key, str)
}, new Map()).values()]

console.log(result)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related