
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Find single in an array of 2n+1 integer elements in C++
- Single dimensional array in Java
- How to Alter Two Array Elements in Java
- Replace the Array Elements with -6 if the Last Digit is 6 in Java
- How to Map multi-dimensional arrays to a single array in java?
- How to sort Java array elements in ascending order?
- How to make elements of array immutable in Java?
- How to use JOptionPane with Array Elements in Java?
- Removing Single Elements in a Vector in Java
- How to sum elements at the same index in array of arrays into a single array? JavaScript
- MySQL query to convert a single digit number to two-digit
- Find Two Array Elements Having Maximum Product in Java?
- Find Two Array Elements Having Maximum Sum in Java?
- Find Array Elements Which are Greater than its Left Elements in Java?
- How to count unique elements in the array using java?
