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

1394 Find Lucky Integer In An Array

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

/*
 * Given an array of integers arr, a lucky integer is an integer
 * which has a frequency in the array equal to its value. Return
 * a lucky integer in the array. If there are multiple lucky
 * integers return the largest of them. If there is no lucky
 * integer return -1.
 *
 * Example 1:
 *
 * Input: arr = [2,2,3,4]
 * Output: 2
 * Explanation: The only lucky number in the array is 2 because
 * frequency[2] == 2.
 */

function findLuckyIntegerInAnArray(nums) {
  let cache = {}
  let luckies = []
  for (const num of nums) {
    cache[num] = (cache[num] || 0) + 1
  }
  for (const key in cache) {
    if (Number(key) === cache[key]) {
      luckies.push(cache[key])
    }
  }
  return luckies.length > 0 ? Math.max(...luckies) : -1
}

test('find Lucky Integer In An Array', () => {
  expect(findLuckyIntegerInAnArray([2, 2, 3, 4])).toEqual(2)
})

Created 2020-04-25T21:18:07+00:00 · Edit