0

How to remove duplicate from string for example

const string = 'PandoraPandora'; to --> Pandora 

OR

const string = 'pandorapandora'; to --> pandora

Note: string doesn't have spaces.

Taher A. Ghaleb
  • 4,810
  • 5
  • 27
  • 41

1 Answers1

0

Here you go:

const str = 'pandorapandora'
const midIndex = Math.floor(str.length/2)
const firstHalf = str.slice(0,midIndex)
const secondHalf = str.slice(midIndex)
const dup = firstHalf == secondHalf ? firstHalf : str
console.log(dup)
Bhojendra Rauniyar
  • 78,842
  • 31
  • 152
  • 211