I have task to receive unique element in order from string as parameter. I do not understand how this function uniqueElements returns [\'A\',\'B\',\'C\',\'B\']
[\'A\',\'B\',\'C\',\'B\']
I would do this O(n) time by utilizing Array.prototype.reduce() as follows;
Array.prototype.reduce()
var str = "AAAABBBBCCBB", uniques = Array.prototype.reduce.call(str, (p,c,i) => i-1 ? p[p.length-1] !== c ? (p.push(c),p) : p : [c]); console.log(uniques);