Hey everyone, I'm working on creating a function that counts the number of identical elements in an array. If the next element is greater than the previous one, 1 is added to the counter. However, I am facing an issue when all elements are identical. In this case, I want the function to add 1 to the counter for each identical element. For example, with [4,4,4,4,4], the output should be 5 since there are 5 identical elements.
function countIdentical(arr) {
let counter = 0;
let counterIdentical = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] < arr[(i + 1) % arr.length]) {
counter++;
}
}
return counter;
}
console.log(countIdentical([4, 3, 3, 2, 1, 2, 2, 1, 1, 3])); //expected output 3
console.log(countIdentical([
[4, 4, 4, 4, 4]
])); //expected output 5