This content originally appeared on DEV Community and was authored by Nirmal Krishna
This is an example implementation using hashmap. The input array nums is considered to have only one unique number found once, other numbers occurs > once.
// ts
function singleNumber(nums: number[]): number {
const hash = {};
for(let i = 0; i< nums.length; i++){
hash[nums[i]] = hash[nums[i]] ? hash[nums[i]] + 1 : 1
}
return Object.keys(hash).filter(k=> hash[k] === 1).map(k=> parseInt(k))[0];
};
This content originally appeared on DEV Community and was authored by Nirmal Krishna
Nirmal Krishna | Sciencx (2021-07-01T03:56:16+00:00) Finding element that appears once in an array where other elements appear twice : Leetcode. Retrieved from https://www.scien.cx/2021/07/01/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-leetcode/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.