-5

I have an array: A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ]

I want to be able to return true in instances where 2 or more consecutive numbers are the same. So in this array, the output array should have 5 trues with [2,2], [0,0], [-1,-1,-1], [-1,-1] and [-1,-1].

So far I have used slice and map on the array through 2 consecutive numbers and have gotten 4 trues.

const solution = (A) => {
    let compare = A.slice(1).map((n,i) => {
       return (n === A[i])
    })

    console.log(compare) // => [ true, false, true, false, false, false, true, true ]
}
const A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ];
solution(A);

But getting that fifth true on the [-1,-1,-1] is eluding me.

Currently the compare output I have is only going through 2 consecutive numbers which is why it's given me 4 trues. My question is basically how to go about checking for the 3 or more consecutive numbers.

My final would be something like

compare.filter(word => word === true).length

to get 5.

TenzingS
  • 63
  • 4
  • I cannot see why those are your expected trues. – jonrsharpe Feb 25 '22 at 17:56
  • What? You array have 4 characters which are, 0, 1, 2, -. So you should get 4 trues, right? And how are you expecting that output. You have repeated -1s 2 times. – Archit Gargi Feb 25 '22 at 17:56
  • Hey @jonrsharpe, so I would get back 4 trues out of 8 possible combinations when checking 2 consecutive numbers (on compare). And 1 true out of a possible 7 combinations when checking 3 consecutive numbers [-1, -1 ,-1]. So total amount of trues should come out to be 5. Since there aren't any 4 or more consecutive numbers that are the same so no more trues. – TenzingS Feb 25 '22 at 18:03
  • @ArchitGargi so the two -1s are the 7-8 and 8-9 combinations. The three -1s are the 7-9 combination – TenzingS Feb 25 '22 at 18:06
  • 1
    Well for one thing you show one triplet when they should all be pairs. But for another there _are_ only four matching pairs of consecutive values, even allowing overlaps. – jonrsharpe Feb 25 '22 at 18:06
  • please show what you mean with consecutive. what is with value `1` (positive)? – Nina Scholz Feb 25 '22 at 18:07
  • Is the number of elements in the result array fixed or dynamic? What are the expected results for `[1, 2, 3, 4]` and `[1, 1, 1, 1]`? – jabaa Feb 25 '22 at 18:09
  • @jonrsharpe yup so there are only 4 when checking 2 consecutive numbers. But I also need to check for 3 consecutive numbers over the entire array again. That's where the last true on the 7th - 9th (-1, -1, -1) combination comes from. Sorry I know I didn't explain it the best! – TenzingS Feb 25 '22 at 18:13
  • Right now your implementation only considers pairs. You should consider windows of other lengths if that's what you want. But why trues and falses - where are the other falses for all of the runs of length 3, 4, 5, ... that aren't the same value? Do you just want the _count_? The _groups_? Your requirements make little sense. – jonrsharpe Feb 25 '22 at 18:16
  • 1
    For `[1, 1, 1, 1]` would you expect 6 - 3 x `[1, 1]`, 2 x `[1, 1, 1]`, 1 x `[1, 1, 1, 1]`? – jonrsharpe Feb 25 '22 at 18:20
  • @jonrsharpe yes for [1,1,1,1]. – TenzingS Feb 25 '22 at 18:27
  • The think the output should be in the format `[ true, false, true, false, false, false, true, true ]`. – Mohsen Alyafei Feb 25 '22 at 18:28
  • 1
    @MohsenAlyafei isn't that something the _OP_ should be telling us? – jonrsharpe Feb 25 '22 at 18:29
  • @TenzingS and what **actual output** should that give? Do you care about what `false`s you get, in what order, or just want six `true`s (and in the latter case why not just... `6`?!) – jonrsharpe Feb 25 '22 at 18:32
  • @jonrsharpe The OP did in the last line of the example code he gave. Not that clear for all of us though. He put like this `console.log(compare) // => [ true, false, true, false, false, false, true, true ]` – Mohsen Alyafei Feb 25 '22 at 18:33
  • @jonrsharpe yup currently it's only checking for 2 consecutives. I'm not sure how to go about with the 3 or more consecutive. – TenzingS Feb 25 '22 at 18:34
  • Get the length `n` of consecutive numbers and push `(n - 1)n/2` `true`s into the array. – jabaa Feb 25 '22 at 18:38
  • @MohsenAlyafei the problem is that's an artifact of their current implementation, which doesn't work. For the other window lengths, it's unclear what if any `false`s they'd expect, what order if any matters, ... Maybe they just want `5`? The problem is somewhat underspecified. Probably the easiest thing would be to create an array of _the actual groups_ starting from each digit, in this case e.g. `[[2, 2], [0, 0], [-1, -1], [-1, -1, -1], [-1, -1]]`. – jonrsharpe Feb 25 '22 at 18:42

1 Answers1

0

Perhaps what would actually be useful is the groups:

const findGroups = (arr) => arr.reduce((result, value, index) => {
  let windowLength = 2;
  while (arr[index + windowLength - 1] === value) {
    result.push(arr.slice(index, index + windowLength));
    windowLength++;
  }
  return result;
}, []);

console.log(findGroups([2, 2, 0, 0, -1, 1, -1, -1, -1]));  // [[2, 2], [0, 0], [-1, -1], [-1, -1, -1], [-1, -1]]
console.log(findGroups([4, 4, 4, 4]));  // [[4, 4], [4, 4, 4], [4, 4, 4, 4], [4, 4], [4, 4, 4], [4, 4]]

That gives you an array of the groups of consecutive values. If you just want 5, it's the .length, or you can calculate it directly rather than building unnecessary arrays:

const findGroupCount = (arr) => arr.reduce((result, value, index) => {
  let windowLength = 2;
  while (arr[index + windowLength - 1] === value) {
    result++;
    windowLength++
  }
  return result;
}, 0);

console.log(findGroupCount([2, 2, 0, 0, -1, 1, -1, -1, -1]));  // 5
console.log(findGroupCount([4, 4, 4, 4]));  // 6
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437