DEV Community

Manvir
Manvir

Posted on

Find Numbers with Even Number of Digits from leetcode

Code flow as follows:
-> I have converted numbers into a string.
-> then find the length of the string
-> If any length is divisible by 2 then push the value to
a new array.
-> return the length of the array.

Alt Text

Top comments (2)

Collapse
 
imjoshellis profile image
Josh Ellis

Good work.

Some optimizations:

If the goal is to return the length of arr at the end, it’s simpler (and less memory) to increment a count number variable rather than pushing to arr.

You also don’t need to reset temp as it will be reset each time the for block loops.

Collapse
 
vishalraj82 profile image
Vishal Raj
var findNumbers = nums => nums.reduce((acc, n) => {
  if (String(n).length % 2 === 0) { acc.push(n); }
  return acc;
}, []);
Enter fullscreen mode Exit fullscreen mode