0

var binary = [1,0,0,1,1,1,0,0,1,0];

I have a function which produces a random array of bits. I would like to combine matching elements and return an array stating how many elements were repeated. The above giving the output = [1,2,3,2,1,1].

Are there any features in JavaScript which would allow me to do this? I have tried searching but not really sure how to describe what I want. Thanks.

2
  • Iterate and count? I mean, it's basic RLE w/o referencing what you're encoding, right? Commented Jun 9, 2018 at 16:38
  • as a side note both arrays can easily be generated in the same loop Commented Jun 9, 2018 at 18:18

5 Answers 5

2

You could use reduce() method and store last element in one variable.

var binary = [1, 0, 0, 1, 1, 1, 0, 0, 1, 0];
let last = null;

var result = binary.reduce((r, e) => {
  if (last == null || last != e) r.push(1);
  else r[r.length - 1]++;
  last = e;
  return r;
}, [])

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

Try following

var binary = [1,0,0,1,1,1,0,0,1,0];

var result = []; // result array
var value = binary[0]; // initial value
var counter = 1; // counter of the values
// Start the loop with 2nd value
for (var i = 1; i < binary.length; i++) {
  // if the value in array is same as stored value, increment counter
  if(value === binary[i]) {
    counter++;
  } else {
  /* if the value is different, push the counter in result 
  ** and reset value and counter */
    result.push(counter);
    value = binary[i];
    counter = 1;
  }
}

console.log(result);

Comments

1

You could check the last element while reducing the array and either push the increased last value or take 1 for concatination.

var binary = [1, 0, 0, 1, 1, 1, 0, 0, 1, 0],
    result = binary.reduce((r, v, i, a) => r.concat((a[i - 1] === v && r.pop()) + 1), []);

console.log(result);

Comments

0

this is the way that i founded to check that binary in array is continuously repeated or not

    var output = [1,0,0,1,1,1,0,0,1,0];
    var result = []
    var counter = 1;
    for(i=0;i<output.length;i++){
        if(output[i] === output[i+1])
            counter++;
        else{
            result.push(counter);
            counter=1;
            }
     }
     console.log(result)

here is an output [ 1, 2, 3, 2, 1, 1 ] and this code is simple to understand

5 Comments

Please provide description to your solution. A post with only code is not complete
first it match first character of array with second if it is matched then counter will increase with 1 (in our case it is not matched ) if not matched push counter in to result which is empty in initially and assign counter with 1 (why counter is 1 ?because if there not matched then at least one character is there )
@alamoot Sometimes I'd agree; in this case, since it's clearly already a homework assignment, adding an explanation makes answering even worse, because it removes even more of the onus on the OP to actually learn.
@Dave, its a personal project, not a homework assignment. Clearly.
@Future I see. Personally I would have spent more time with it before giving up, and included your best effort(s) in the question as well, but that's just me. And it's a very odd "personal project", but I have odder ones, so who am I to judge.
0

A bit shorter and slower alternative:

console.log( [1,0,0,1,1,1,0,0,1,0].join('').match(/0+|1+/g).map(m => m.length) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.