How to Find Single Digit Array Elements in Java?


In a given array with some random integer values, we have to find out the single digits available in the given array and print those single digit values as output.

An array can contain positive or negative numbers irrespective of the number of digits in a number. As here all elements belong to numeric type.

Note- Take a positive integer array.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

Given Array= [1, 12, 3, 4, 10, 15].

The single digit elements present in the given array = 1, 3, 4

Instance-2

Given Array= [451, 102, 3, 14, 100, 15].

The single digit elements present in the given array = 3

Instance-3

Given Array= [111, 612, 83, 4, 10, 5, 9, 89].

The single digit elements present in the given array = 4, 5, 9

Algorithm

Algorithm-1

Step 1 − Declare an array with some random integer values by static input method.

Step 2 −Take a for loop in which we check for a condition that the number lies between 0 and 9 or not.

Step 3 − If the condition is satisfied, we confirm that the number is a single digit.

Step 4 − Print those single digit numbers as output.

Algorithm-2

Step 1 − Declare an array with some random integer values by static input method.

Step 2 − Initiate a loop in which we check for a condition that the modulus value with respect to 10 of that number is equal to the same number or not. It concludes that the number is a single digit number.

Step 3 −If the condition is satisfied, we confirm that the number is a single digit.

Step 4 −Print those single digit numbers as output.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Simple Digit Check Method

  • By Using Modulus Check method

Let’s see the program along with its output one by one.

Approach-1: By Using Simple Digit Check Method

In this approach, we declare an array with some random integer values and by using our algorithm we find the single digit number and print those numbers as output.

Example

public class Main {
   public static void main (String[] args) {
     
      //declare an integer type array 
      //and store some random positive integer values
      int inputArray[] = {7, 12, 5, 9, 15};
      
      //declare an integer value for counting single digit elements
      //and initialize it with 0
      int count = 0;
      System.out.print ("Single digit elements present in array are: ");
      
      //initiate the loop to find the single digits
      for (int i = 0; i < inputArray.length; i++)  {
        
         //take a for loop to check every values
         if (inputArray[i] >= 0 && inputArray[i] <= 9){
           
            //If the number satisfied above condition
            //its a single digit number so print that number
            System.out.print(inputArray [i] + " ");
            
            //increment the count value if any single digit found
            count+=1;
         }
      }
      
      //if no single digit detected
      if(count==0){
     
        //print not available as output
         System.out.print(" N/A ");
      }
   }
}

Output

Single digit elements present in array are: 7 5 9

Approach-2: By Using Modulus Check Method

In this approach, we declare an array with some random integer values and by using our algorithm we find the single digit number and print those numbers as output.

Example

public class Main {
   public static void main (String[] args) {
      
      //declare an integer arrays and store some random +ve integer values
      int inputArray1[] = {20, 12, 33, 2, 11, 3};

      //declare an integer value for counting single digit
      //initialize it with value 0
      int count = 0;
      System.out.print ("Single digit elements present in the array are: ");

      //take a for loop to find the single digits in first array
      for (int i = 0; i < inputArray1.length; i++) {
        
         //in each loop find the modulus value with 10
         //so that we can find the single digit value
         if (inputArray1[i] % 10 == inputArray1[i]){
            //If the number satisfied above condition its a single digit number
            System.out.print(inputArray1 [i] + " ");
             
            //increment the count value if any single digit found
            count+=1;
         }
      }

      //if no single digit detected
      if(count==0){
         //print not available as output
         System.out.print(" N/A ");
      }
   }
}

Output

Single digit elements present in the array are: 2 3

In this article, we explored different approaches to find single digit elements in an array by using Java programming language.

Updated on: 09-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements