FastPrepThe Only Nonrepeating Array Element

The Only Nonrepeating Array Element

Mygate logoMygate● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a nonempty integer array nums, return its only value that occurs exactly once.

The input contains exactly one such value. Every other distinct value occurs at least twice, and those repeated values may have different frequencies. The array is not necessarily sorted.

Function

onlyNonrepeatingElement(nums: int[]) → int

Examples

Example 1

nums = [4,1,4,2,1]return = 2

Only 2 occurs once.

Example 2

nums = [-1,8,8,8,-1,-1,5]return = 5

The repeated values each occur three times; only 5 occurs once.

Example 3

nums = [9]return = 9

A singleton has exactly one value occurring once.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Exactly one value has frequency 1; every other distinct value has frequency at least 2.

More Mygate problems

See Mygate hiring insights
public int onlyNonrepeatingElement(int[] nums) {
    // write your code here
}
nums[4,1,4,2,1]
expected2
Checking account…