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
  • 5,120
  • 5
  • 31
  • 44

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
  • 83,432
  • 35
  • 168
  • 231