Now displays the number of repeating elements (number 3). And you also need to withdraw it [2, 2, 2]

var arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; var q = 1; var e = -1; var max = -1; var pos = -1; for (var i = 0; i < arr.length - 1; i++) { if (arr[i] == arr[i + 1]) { q++; if (max < q) { max = q; pos = i; e = arr[i]; } } else { q = 1; } } console.log(max); 

  • it seems that the text of the question is a bad machine translation - Igor
  • changed the question incorrect - recile
  • What exactly should be displayed? Can you give an example of the correct result? - Telion
  • owes it to himself [2, 2, 2], and now it derives its quantity, i.e. 3 - recile

1 answer 1

 var arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; var q = 1; var e = -1; var max = -1; var pos = -1; for (var i = 0; i < arr.length - 1; i++) { if (arr[i] == arr[i + 1]) { q++; if (max < q) { max = q; pos = i; e = arr[i]; } } else { q = 1; } } console.log(max); console.log(Array(max).fill(e));