Counting of similar objects in the vector

Asked

Viewed 21 times

0

I would like to know how to count sequences of different within a vector containing only 0 and 1.

For example, in the vector

x <- c(1, 1, 1, 0, 0, 0, 1, 1)

the count would give the vector 3 why he counts equal sequence.

"1, 1, 1", "0, 0, 0", "1, 1" = 3
  • 1

    Can you specify your question better? Are you using any specific language? How is the data structure (more practical) you get to validate?

  • @Breno, you’ve solved the problem?

1 answer

1

Whereas a sequence can also be formed by an element, you can use the following:

int valorAtual = -1;
int total = 0;
for (int i=0 ; i<c.count() ; i++) {
  if (valorAtual != c[i])
    total++;
  valorAtual = c[i];
}

NOTE: this algorithm is generic, since it did not inform the language

Browser other questions tagged

You are not signed in. Login or sign up in order to post.