## Approach

Calculate 2 prefix leaders, for the array and its reversed

The result is number of equal pairs

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}function solution(A) {2  const getPrefixLeader = A => {3    let maxOcc, maxEl4
5    let prefixLeader = Array(A.length)6    maxOcc = maxEl = -Infinity7    A.reduce((acc, el, i) => {8      const occ = (acc[el] || 0) + 19      acc[el] = occ10      if (maxOcc < occ) {11        maxOcc = occ12        if (occ > Math.floor((i + 1) / 2)) {13          maxEl = el14        }15      }16      if (!(maxOcc > Math.floor((i + 1) / 2))) {17        maxEl = -118      }19
20      prefixLeader[i] = maxEl21      return acc22    }, {})23
30  return prefixLeader.filter(31    (p, i) => p !== -1 && p === prefixLeaderReversed[i + 1]32  ).length33}```

codility

## Next Post

Codility: Dominator

Jan 24, 2021