in the worst case (outlier is at the end of the loop) it will go through all items. but if the outlier is found before, the loop stops.
functionfindOutlier(nums){letlastOddOrEven=[null,null];for(leti=0;i<nums.length;i++){constparity=nums[i]%2;if(lastOddOrEven[0]!==null&&lastOddOrEven[1]!==null){// we've already seen one odd and one even number,// so the outlier is the one number we've seen with a// different parityreturnlastOddOrEven[1-parity];}lastOddOrEven[parity]=nums[i];}// if the loop can't return the outlier, this is because it's the last itemreturnnums[nums.length-1];}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
in JavaScript. o(n), uses constant space.
in the worst case (outlier is at the end of the loop) it will go through all items. but if the outlier is found before, the loop stops.