3

I'm trying to do something interesting in JavaScript, but I can't. This is my input:

 var Input = ['a','a','a','b','b','b','b','c','c','c','a','a','c','d','d','d'];

So my output is that only get differents values and go in a new vector.

 var Output = SomeFunction(Input);

This is what I want:

Output = ['a','b','c','a','c','d'];

Y tried with this, but don't work aswell:

function SomeFunction(input){
var out= [];
 for (var i = 0; i < input.length - 1; i++) {
  if(input[i] == input[i+1]){
   out.push(input[i]);       
  }
 }
 return out;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Alberto Acuña
  • 512
  • 3
  • 9
  • 28
  • 1
    I think it should be `if(input[i] != input[i+1])` and check the last element separately. – Passerby Oct 14 '15 at 08:47
  • 1
    If you only want the unique values you can try this http://stackoverflow.com/questions/1960473/unique-values-in-an-array – Yvo Cilon Oct 14 '15 at 08:47
  • Is the input an array or a string, and you've converted the string to array? – Tushar Oct 14 '15 at 08:55
  • I'd loop over `input` with a for loop, and keep an index `prev` pointing to the index in `input` of the value most recently added to `out` (starting with the first value). Then only add a new value and advance `prev` to the current index the value at the current index is different than the value at index `prev`. – aaroncarsonart Oct 14 '15 at 08:55

9 Answers9

5

You can use filter()

var input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd'];


input = input.filter(function(v, i, arr) {
  return arr[i - 1] !== v;
  //compare with the previous value
})

document.write(JSON.stringify(input));
Tushar
  • 85,780
  • 21
  • 159
  • 179
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
2

Try like this

var out= [];
var i = 0;
 for (i = 0; i < input.length - 1; i++) {
  if(input[i] != input[i+1]){
   out.push(input[i]);       
  }
 }
if (out[out.length-1] !== input[i]) 
   out.push(input[i]);     
Tushar
  • 85,780
  • 21
  • 159
  • 179
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
2

You can use backreferencing regex

  1. Convert the array to string by using join, so that regex can be used on it
  2. Use backreferencing regex to remove the consecutive elements with replace()
  3. Convert back the string to array using split

(\w)\1* Explanation

var input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd'];
var str = input.join('');

input = str.replace(/(\w)\1*/g, '$1').split('');

console.log(input);
document.write('<pre>' + JSON.stringify(input, 0, 2) + '</pre>');
Tushar
  • 85,780
  • 21
  • 159
  • 179
2

You can do a filter like

var Input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd', 'e'];

var Output = SomeFunction(Input);

function SomeFunction(input) {
  var out = input.filter(function(value, i) {
    return value !== input[i + 1]
  });
  return out;
}

output.innerHTML = JSON.stringify(Output)
<pre id="output"><pre>
Tushar
  • 85,780
  • 21
  • 159
  • 179
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

var Input = ['a','a','a','b','b','b','b','c','c','c','a','a','c','d','d','d'];
var Output = SomeFunction(Input);
function SomeFunction(input){
var out= [];
 for (var i = 1; i < input.length; i++) {
  if(input[i] != input[i-1]){
   out.push(input[i-1]);       
  }
 }

 out.push(input[input.length - 1]);
 return out;
}

alert(Output);
Amit
  • 45,440
  • 9
  • 78
  • 110
1

How about this:

var Input = ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'c', 'd', 'd', 'd'];

function SomeFunction(input) {
  var out = [];
  var initStr = input[0];
  console.log(initStr)
  for (var i = 1; i < input.length; i++) {
    if (input[i] === initStr) {


    } else {
      out.push(input[i - 1]);
      initStr = input[i];
    }
  }
  out.push(input[i - 1]);
  console.log(out);
}

SomeFunction(Input)
Sandeep Nayak
  • 4,649
  • 1
  • 22
  • 33
1

Example using array filters.

Pretty simple as long as the array is not too large and could rather easily be extended to compare some property in an array of objects. With a larger array it might be faster though to do a for loop instead as in some of the other answers.

var Input = ['a', 'b', 'c','a', 'b', 'a', 'b', 'c','c']
var Output = Input.filter(function(value,index) { return Input[index - 1] != value; });
Jonas Pegerfalk
  • 9,016
  • 9
  • 29
  • 29
1
function SomeFunction(input) {
var out= [];
   out.push(input[i]);         
   for (var i = 0; i < input.length - 1; i++) {
      if(input[i] !== out[out.length-1]){
         out.push(input[i]);         
      }
   }
   return out;
}
Tushar
  • 85,780
  • 21
  • 159
  • 179
1

Worked for me:

function SomeFunction(input){
var out= [];
 for (var i = 0; i < input.length; i++) {
  if(input[i] !== input[i+1]){
   out.push(input[i]);       
  }
 }
 return out;
}

Be careful with the name of the varible "input". It's not "Input", it's "input".

Tushar
  • 85,780
  • 21
  • 159
  • 179
Mundo
  • 96
  • 7