Java program to find repeated digits in a number

In our earlier post we learn about redundant digits in an array.Today we shall how to find the redundant digits in any given number.This program is bit complicated since we use lot of if,for,while conditions.
The process of finding the number involves the below stated stages.

1. Input
2 .Getting the list of digits present in the given number
3. Picking up each element in the list and then checking the number whether it is present in the list minimum of two times
4. If in the 3 stage the number is present for a minimum of two times then we shall add that number to an array of redundant elements.
5. If in 3 stage the number is present only once in list then we need not to add that number to the redundant elements array.
6. While adding the elements to the redundant elements we need to check whether the element is already present in the array,if not we can add it else we shall skip.

Terminology used in the program:
1. INPUT:The variable number is used to store the input from the user.
2. CLASS OBJECTS:in,ob are used as objects for the class Scanner and finding respectively.
3. ARRAYS:listofdigits is an array used to store the digits present in the program.redunele is an array used to store the redundant elements in the number.
4. Miscellaneous variables:k,i,r,flag,temp.
5. Methods: listingofdigits is a method which we use to find the digits present in the number,finding is a method where we check for the redundant elements in the listofdigits and add them to the array.
The code of the problem is:

import java.util.*;
class finding
 int len,a=0;
 int listofdigits[] = new int[10];
 int redunele[] = new int[10];
 int k,i,r=0,flag=0;
 void listingofdigits(int number)
    int temp=number;
          r = temp%10;
          temp = temp/10;
        } //while end
     } //for end
  } // function close
 void finding(int number)
    int temp=number;
        r = temp%10;
        temp = temp/10;
           if(listofdigits[i] == r)
          } //for end
          for(int j=0;j<len;j++)
               } // else close 
            } //for close
         } // if close
    } // while end
 } // function end
void display()
        System.out.println("No redundant elements:");
       System.out.println("Redundant elements are:");
       for(int i=0;i<a;i++)
        System.out.print(redunele[i]+" ");
      } //else close
 } //function close        
} //class end
class numberredun2
   public static void main(String arg[])
        Scanner in = new Scanner(;
        System.out.println("Enter any number:");
        int n = in.nextInt();
        finding ob = new finding();
        ob.len = String.valueOf(n).length();
        System.out.println("Lengtht is " + ob.len);

1.In the main method we get a number as input from the user.
2.Then we call the method listingofdigits by passing the number as parameter.
3.In this method we list out what are the digits present in the given number. Eg:If the input is 132 then the digits in it are 1,3,2.
4.Then we invoke the method findig.In it select a number in the array of listofdigits and search for the same number.If the number is present more than one then add it to redunele array.If the element is not present then ignore it.
5.Finally display the elements present in the redunele array.

The output of the program is.

You can download the code.
You can like,follow our blog.


About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s