Finding element that appears once in an array where other elements appear twice : Leetcode

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…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Finding element that appears once in an array where other elements appear twice : Leetcode." Nirmal Krishna | Sciencx - Thursday July 1, 2021, https://www.scien.cx/2021/07/01/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-leetcode/
HARVARD
Nirmal Krishna | Sciencx Thursday July 1, 2021 » Finding element that appears once in an array where other elements appear twice : Leetcode., viewed ,<https://www.scien.cx/2021/07/01/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-leetcode/>
VANCOUVER
Nirmal Krishna | Sciencx - » Finding element that appears once in an array where other elements appear twice : Leetcode. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/01/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-leetcode/
CHICAGO
" » Finding element that appears once in an array where other elements appear twice : Leetcode." Nirmal Krishna | Sciencx - Accessed . https://www.scien.cx/2021/07/01/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-leetcode/
IEEE
" » Finding element that appears once in an array where other elements appear twice : Leetcode." Nirmal Krishna | Sciencx [Online]. Available: https://www.scien.cx/2021/07/01/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-leetcode/. [Accessed: ]
rf:citation
» Finding element that appears once in an array where other elements appear twice : Leetcode | Nirmal Krishna | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.