Find Numbers with Even Number of Digits

developerlife.com

This is a Leetcode Data Structure / Algorithm problem, to solve this problem you need to understand what an even number is.

Even Number: is a number that is divisible by two with no remainders left

Problem

Given an array of integers, return how many of them contain an even number of digits.

Example 1:

Input: nums = [12,345,2,6,7896]
Output: 2
Explanation:
12 contains 2 digits (even number of digits).
345 contains 3 digits (odd number of digits).
2 contains 1 digit (odd number of digits).
6 contains 1 digit (odd number of digits).
7896 contains 4 digits (even number of digits).
Therefore only 12 and 7896 contain an even number of digits.

First Step: you add an Int variable that will act as a counter, which will hold the number of even digits.

var count = 0

Second Step: you would loop through the array to get the individual elements, then get the count of digits

This will get the count even with a negative number(val numlength = u.absoluteValue.toString().length)

for (u in ty) {

val numlength = u.toString().length

Third Step: check if the value is an even number, divide the length by 2. if the remainder is 0, it is even

if (numlength % 2 == 0) count++

Fourth Step: Return the count value

return count
Leetcode submission

Thanks for reading.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nwokocha Wisdom Maduabuchi

A software engineer with considerable experience in mobile development, native Android, and IOS development(Xcode), flutter dev, technical writing and community