germancin
6/1/2018 - 6:05 AM

This will remove dups then transform the object into an array to later joining it.

This will remove dups then transform the object into an array to later joining it.

function removeDupes(str) {
    let newStr = new Set(str) // O(n)
    newStr = Array.from(newStr) // O(n)
    return newStr.join('') // O(n)
}

console.log(
  removeDupes('abcd'),          // abcd
  removeDupes('aabbccdd'),      // abcd
  removeDupes('abababcdcdcd'), // abcd

)

// Time: O(3n) => O(n)
// Space: O(n)