-2

So i have a column of data which consists of numbers where i have to find 5 instances of the same number.

So ,for example, 8,8,8,8,8,8,8,8,8,9,9,9,5,6,4,7,,6,2,3, etc. In this 8 has occurred 9 times. So i want to increment the count only once because even though there are nine 8's what my code is doing is taking the first 8 and getting the 5 consecutive numbers are incrementing. Then it takes the next 8 and increments the count and so on where the count becomes 5 but i want it to be 1. What i want is the first occurrence of any number to be the base value and take 5 consecutive numbers and increment the count. Then then take the 6th 8 and count if there are 5 consecutive 8's or that specific number or not. So, for example, 8,8,8,8,8,8,9,9,9,9,9,9,9,9,5,5,5,5,5,1,1,2,2,5,4,3,6,7,9,3,4,2,2,2,2,2,1,2,1. In this the count should be 4.

   **count=0;
count1=0;
for i=1:length(data)-4
for j=i+1:i+4
if data(i)~=data(j)
count1=0;
break;
else
count1=count1+1;
end
if count1==5    % line 0
count=count+1;
%data(i,1)=data(i+5,1); //line 1   <=comment
%data(i)=data(i+5);     //line 2   <=comment
else
continue;
end
end**

If(count_consecutive==5){
count_main=count_main+1; ...
a[i]=a[i+5];// continue for the base value. It should skip all the numbers that were counted in the consecutive count one and take the next number as the base for counting the consecutive numbers}

The logic in any language would be fine as my error is in the logic Thanks for any help. It would be greatly appreciated :).

Extra Elaboration

So the first one only has 5 consecutive 8's in the manner i specified. Hence the first one will have an output count =1 . In the second one, there are 4 of the 5 consecutive numbers of the same starting number. Hence the output would be 4, Another example i can give is 8,8,8,8,8,8,8,8,8,8(ten 8's),9,9,9,9,4,5,6,4,6,6,6,6,6. In this , the count should be 3 as it has 10 8's which increments the count to 2 and another 5 consecutive 6 which increments count one more time. Total count would be 3.

Now the error is i can't jump the array index from a[i] to a[i+5]. So the first one only has 5 consecutive 8's in the manner i specified. Hence the first one will have an output count =1 . In the second one, there are 4 of the 5 consecutive numbers of the same starting number. Hence the output would be 4, Another example i can give is 8,8,8,8,8,8,8,8,8,8(ten 8's),9,9,9,9,4,5,6,4,6,6,6,6,6. In this , the count should be 3 as it has 10 8's which increments the count to 2 and another 5 consecutive 6 which increments count one more time. Total count would be 3. My problem is that i'm not able to skip/jump the array index from x to x+5 in the for loop when my condition gets satisfied.

  • 2
    `Now the error is i can't jump the array index from a[i] to a[i+5]` - why not? After you find 5 consecutive numbers, you can increment the index of the loop (i) by 4 (since the loop already increments it by 1 anyway) – Eran Mar 01 '21 at 09:16
  • 2
    *`The logic in any language would be fine as my error is in the logic`* - Frankly I'm not surprised, the paragraph explaining the logic is very unclear. If an algorithm isn't defined clearly, it is going to be very hard to produce as code! Please [edit] your question to include a [mcve], specifically step us through (with an example not words) what you expect to happen for a simple input with its expected output. – Wolfie Mar 01 '21 at 10:15
  • So you want to count runs of equal values with minimum length 5? (Your question is not clearly worded). Does a run of say 11 equal values count once or twice? – Luis Mendo Mar 01 '21 at 13:01

3 Answers3

1

I attach the code in Matlab (also works in Octave):

vector = [8,8,8,8,8,8,9,9,9,9,9,9,9,9,5,5,5,5,5,1,1,2,2,5,4,3,6,7,9,3,4,2,2,2,2,2,1,2,1]

count = 1;
result = 0;
for i = 2:length(vector)
    if vector(i-1) == vector(i)
        count = count+1;
    else
        count = 1;
    end
    if count == 5
        result = result+1;
        count = 1;
    end
end

Mainly, you have to count the number of times that a value appears and increase the result if this number of times arrises 5.

PedroRodriguez
  • 368
  • 2
  • 9
0

It would be simpler to count the length of the subsequence containing the same elements consecutive and as soon as the subsequence is finished, to increment the resulting counter by the number of groups consisting of N elements in the subsequence: result += consecutive / n:

public static int countConsecutiveN(int n, int ... arr) {
    if (null == arr || arr.length < n) {
        return 0;
    }
    int result = 0;
    int previous = arr[0];
    int consecutive = 1;
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] == previous) {
            consecutive++;
        } else { // consecutive sequence ended
            result += consecutive / n; // increment by the count of N elements in the subsequence
            consecutive = 1;
        }
        previous = arr[i];
    }
    // check the trailing subsequence
    result += consecutive / n;

    return result;
}

Test

System.out.println(countConsecutiveN(5,
        8,8,8,8,8,8,            // six 8s
        9,9,9,9,9,9,9,9,9,9,    // ten 9s - 2 groups
        5,5,5,5,5,              // five 5s
        1,1,2,2,5,4,3,6,7,9,3,4,
        2,2,2,2,2,2,2,          // seven 2s
        1,2,
        1,1,1,1,1,1             // six 1s
));

Output

6
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
0

If you want to count runs of equal values with a minimum length N, in Matlab this can be done very easily with diff (consecutive differences) and find (indices of nonzero entries):

N = 5; % mininum desired run length
x = [8,8,8,8,8,8,9,9,9,9,9,9,9,9,5,5,5,5,5,1,1,2,2,5,4,3,6,7,9,3,4,2,2,2,2,2,1,2,1];
result = sum(diff(find([true diff(x)]))>=N);

If a run ends immediately when reaching length N (so for example 11 consecutive equal values count as two runs of length N=5):

result = sum(floor(diff(find([true diff(x)]))/N));
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147