136. Single Number Leetcode Solution

Single Number Leetcode Problem :

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

 

jump game leetcode

Single Number Leetcode Solution :

Constraints :

  • 1 <= nums.length <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4
  • Each element in the array appears twice except for one element which appears only once.

Example 1:

  • Input: nums = [4,1,2,1,2]
  • Output: 4

Example 2:

  • Input: nums = [1]
  • Output: 1

Intution :

We can do this using unordered_map as we need to count the frequency of element.

Approach :

  • Make an unordered map in which store the count of all values present in an array.
  • Traverse through map and check which value has count 1 and then break through loop and store that value in ans variable.
  • Return ans.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription