JSFiddle - React, Tailwind, and code Playground

by mariomc

JavaScript

function solution( A, n ){
  var instancesOfN = [];
  var noninstancesOfN = [];
  for( var i = 0; i < A.length; i++ ){
    var symmetricalIndex = A.length - 1 - i;
      instancesOfN[i] = ( instancesOfN[i - 1] || 0 ) + ( A[i] === n ? 1 : 0 );
      noninstancesOfN[symmetricalIndex] = ( noninstancesOfN[symmetricalIndex + 1] || 0 ) + ( A[symmetricalIndex] !== n ? 1 : 0 );
    
    if( typeof instancesOfN[i] !== 'undefined' && typeof noninstancesOfN[i] !== 'undefined' && instancesOfN[i] === noninstancesOfN[i] ) {
      return i + 1;
    }
    if( typeof instancesOfN[symmetricalIndex] !== 'undefined' && typeof noninstancesOfN[symmetricalIndex] !== 'undefined' && instancesOfN[symmetricalIndex] === noninstancesOfN[symmetricalIndex] ) {
      return symmetricalIndex + 1;
    }
  }
}

var sol = solution( [1,0,0], 1);
window.cenas = solution;