-1

The function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.

For example:

uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] 
uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] 
uniqueInOrder([1,2,2,3,3]) == [1,2,3]
m0meni
  • 16,006
  • 16
  • 82
  • 141
m.mohit7
  • 19
  • 2
  • Please share your code thus far. – Will Mar 27 '17 at 23:12
  • Possible duplicate of [Remove Duplicates from JavaScript Array](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array) – Heretic Monkey Mar 27 '17 at 23:14
  • @Kinduser now I have – m0meni Mar 27 '17 at 23:19
  • @Kinduser "way different"? he's asking how to remove duplicates from an array. Is it just the fact that it's a string? Because there are many duplicates to that too. – Heretic Monkey Mar 27 '17 at 23:20
  • @MikeMcCaughan the question was phrased quite poorly. You don't remove all dupes, but rather only the ones that are next to each other. – m0meni Mar 27 '17 at 23:21
  • 1
    @Kinduser or the fact that you didn't explain how your code works... which I believe I've mentioned on more than one of your answers before... – Heretic Monkey Mar 27 '17 at 23:22
  • So you want contiguous uniqueness? – vol7ron Mar 27 '17 at 23:24
  • And there are many duplicates of the recently edit question as well. http://stackoverflow.com/q/37668775/215552 for instance, the only difference being returning a string rather than an array. Of course, @Kinduser you seem to want to answer any and all questions irrespective of their topicality, I'm out. – Heretic Monkey Mar 27 '17 at 23:25
  • @MikeMcCaughan Once again, the link you have provided includes **quite different** problem and **quite different** solution. – kind user Mar 27 '17 at 23:27

2 Answers2

2

What you want to do first is normalize your input such that whether it's a string or an array, it doesn't matter.

const input = Array.isArray(x) ? x : x.split('');

After the above, the input will always be an array. Now the logic for skipping is duplicates is quite simple

for (let i = 0; i < input.length; ++i) {
  if (input[i] == input[i + 1]) continue
  result.push(input[i])
}

You continue, i.e. skip each index in the input that is the same as the next for each index. Then for each element that isn't duplicated, you push into your result array.

The whole thing looks like this:

function uniqueInOrder(x) {
  const result = [];
  const input = Array.isArray(x) ? x : x.split('');

  for (let i = 0; i < input.length; ++i) {
    if (input[i] == input[i + 1]) continue
    result.push(input[i])
  }
  
  return result
}

console.log(uniqueInOrder('AAAABBBCCDAABBB'));
console.log(uniqueInOrder('ABBCcAD'));
console.log(uniqueInOrder([1, 2, 2, 3, 3]));

If you're familiar with the filter function you can make it a one liner.

function uniqueInOrder(x) {
  return (Array.isArray(x) ? x : x.split(''))
    .filter((c, i) => c !== x[i + 1]);
}

console.log(uniqueInOrder('AAAABBBCCDAABBB'));
console.log(uniqueInOrder('ABBCcAD'));
console.log(uniqueInOrder([1, 2, 2, 3, 3]));
m0meni
  • 16,006
  • 16
  • 82
  • 141
0

Offered is two different solutions, depending on what you want:

  1. You can use Set() for unique sets. Simply adding the value to a set will retain the order and prevent unique values from being added.

  2. For contiguously unique sets, you can use filter to iterate over an array.

    • If the argument is a string, this array is created by splitting on a blank string (.split(''))
    • When iterating over the array of input values, you can compare the current value to the previous; if they are different then return true
    • A true response will include the value in the array you will be creating as a result of the filter (which holds your unique values)

uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
uniqueInOrder([1, 2, 2, 3, 3]) == [1, 2, 3]



function unique(input) {
  var set = new Set(),
    arr = input;
    
  if (typeof input == 'string')
    arr = arr.split('');

  arr.forEach(val => {
    set.add(val)
  });

  console.log('input:',input);
  console.log('unique:',[...set]);
  return [...set];
}

function uniqueInOrder(input) {
  var arr = input;
  if (typeof input == 'string')
    arr = arr.split('');

  var unique = arr.filter((val,i,arr)=>{
    return val !== arr[i-1];
  });

  console.log('input:', JSON.stringify(input));
  console.log('uniqueInOrder:', JSON.stringify(unique));
  return unique;
}
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • @Kinduser I don't think your username is quite accurate. Moreover if you want to talk about overcomplication your usage of `[...str]` everywhere is just as bad if not worse considering it turns your algorithm into an `O(n^2)` one. – m0meni Mar 27 '17 at 23:45
  • @Kinduser because just posting working code doesn't mean you answered a question properly. If you want a legitimate critique, your code initially had very poor runtime, which you've now fixed, and you still don't really explain it. Moreover, you haven't been very pleasant in your comments. Maybe try taking a break from this question? You seem upset. – m0meni Mar 27 '17 at 23:52