code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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