Day 11 of 30 - Ruby Coding Challenge - Repdigit Number Algorithm

DZone 's Guide to

Day 11 of 30 - Ruby Coding Challenge - Repdigit Number Algorithm

Day 11 of 30. We're going solve the the Repdigit Algorithm, which validates if a digit is repeated in the whole number

· Web Dev Zone ·
Free Resource


This is the blog post version of the Youtube video of the Ruby Coding Challenges in 30 days.

Today we’re going to solve this simple and elegant coding challenge, which is the Repdigit Algorithm:

Given a number, I want to check if the number is composed of repeated instances of the same digit

That was the fancy definition.

Real Life:


This is a Repdigit (repeated digit) because it repeats 9 for the whole number


This is a Repdigit because it repeats 7 for the whole number


This is not a Repdigit because it doesn’t repeat 6 for the whole number


This is not a Repdigit because it doesn’t repeat 3,4 or 7 for the whole number

I think you got it. Very simple.

#1 Repdigit Algorithm Solution

One possible solution is

  • convert the number into an array
  • take the first digit of the number
  • check if all of the numbers are the same digit

Pretty simple, right?

Let’s see how Ruby handles it.

Step 1

The first step is actually to convert the Integer into a String so that we can be able to manipulate it


Step 2

Now it’s time to create the array based on that string


Step 3

Finally, let’s convert each string in the array back to an integer


Step 4

I’m going to take the first digit to compare with all the numbers


Step 5

Then the final step is to count how many digits in that number are different from the first digit


Perfect! If the count variable is different from zero then we have a digit that is different from the first digit, which means that the number is not Repdigit

The final solution would be:


#2 A Leaner Solution in Ruby

A little bit leaner solution would be the following:


That’s it!

In the next video, we’re going to explore a Ruby Way to solve this algorithm. See you there :)

Don’t forget to come by and say hi Alex
Twitter Youtube Instagram Linkedin GitHub Courses

algorithm, algorithm analysis, coding, programming, programming language, rails, ruby, tutorial for beginners

Published at DZone with permission of Alexandre Gama . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}