How can I get the original type of data after being processed by Object as Hash in JavaScript? -


leetcode 136 single number given array of integers, every element appears twice except one. find single one.

i have solved problem using object-property in js hash. however, property allows string type, data automatically converted string.

var singlenumber = function(nums) { var hash = {} for(var i=0;i<nums.length;i++)     if (hash.hasownproperty(nums[i]))         delete hash[nums[i]]     else         hash[nums[i]] = 1 for(var x in hash)     return number(x) } 

how can original type of data after processing? in problem use number() convert.

however, if array in problem not contains number, string. ['123',123,...] may fail solution above.

it done, store object instead of 1

https://jsfiddle.net/uo8v55qf/1/

var singlenumber = function(nums) { var hash = {} for(var i=0;i<nums.length;i++)     if (hash.hasownproperty(nums[i]))         delete hash[nums[i]]     else         hash[nums[i]] = { value:nums[i], type: typeof nums[i]}; for(var x in hash)  return hash[x]; }     result = singlenumber(['123',123,'23']); console.log(result); console.log(result.value + ' type:' + result.type); //23 type:string  result = singlenumber(['123',123,23]); console.log(result.value + ' type:' + result.type); //23 type:number 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -