DEV Community

chandra penugonda
chandra penugonda

Posted on

N Repeated Element - THE DAILY BYTE

Today's Byte

You are given an integer array, nums, which contains 2N elements. Within nums there are N + 1 unique elements and a specific element occurs N times. Return the element within nums that appears N times.

Ex: Given the following nums…
nums = [3, 3, 5, 1], return 3.

Ex: Given the following nums…
nums = [4, 2, 4, 5, 4, 1], return 4.

Intuition

Count number of elements using HashMap and return element when count > 1

Solution

const repeatedNTimes = function(nums) {
    const map = new Map()
    for(let i=0;i<nums.length;i++){
      if(map.has(nums[i])) return nums[i]
      else map.set(nums[i],true)
    }
};
Enter fullscreen mode Exit fullscreen mode

Complexity Analysis :
Time Complexity: O(N)
Space Complexity: O(N)

Top comments (0)