code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

1287 Element Appearing More Than 25Percent In Sorted Array

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * Given an integer array sorted in non-decreasing order, there is exactly
 * one integer in the array that occurs more than 25% of the time.
 * 
 * Return that integer.
 * 
 * Example 1:
 * 
 * Input: arr = [1,2,2,6,6,6,6,7,10]
 * Output: 6 
 */

function elementAppearingMoreThanInSortedArray(nums) {
  for (let index = 0; index < nums.length; index++) {
    if(nums[index] === nums[Math.floor(nums.length*0.25) + index]){
      return nums[index]
    }    
  }
  return -1
}

test('element Appearing More Than 25% In Sorted Array', () => {
  expect(elementAppearingMoreThanInSortedArray([1,2,2,6,6,6,6,7,10])).toEqual(6)
});

Created 2020-04-19T21:57:04+00:00 · Edit