LeetCode: Single Number Solution
Count occurence of each elementApproach
Use a hash table (Map) to store key-value pairs of number and its occurence
Implementation
1var singleNumber = function (nums) {2 const numOccMap = nums.reduce(3 (acc, el) => acc.set(el, (acc.get(el) || 0) + 1),4 new Map()5 )67 for (const [num, occ] of numOccMap) {8 if (occ === 1) {9 return num10 }11 }12}
References
Comments
Loading comments...
Tags
leetcode
hash table
bit manipulation
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Previous Post
LeetCode: Rotate Array
Stale solution might be time-limit exceeded someday