
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Array Elements with At Least One Smaller Element in Java
An array is a linear data structure in which elements are stored in contiguous memory locations.
As per problem statement, we need to print all elements that have at least one smaller element. In simple terms we can say printing all elements of the array except the smallest one as the smallest one does not have any smaller element than this.
Let's explore the article to see how it can be done by using Java programming language.
To Show You Some Instances
Instance-1
Suppose we have the below array [10, 2, 3, -5, 99, 12, 0, -1] Now all elements that have at least one smaller element are = 10, 2, 3, 99, 12, 0, -1
Instance-2
Suppose we have the below array [55, 10, 29, 74, 12, 45, 6, 5, 269] Now all elements that have at least one smaller element are = 55, 10, 29, 74, 12, 45, 6, 269
Instance-3
Suppose we have the below array [556, 10, 259, 874, 123, 453, -96, -54, -2369] Now all elements that have at least one smaller element are = 556, 10, 259, 874, 123, 453, -96, -54
Algorithm
Algorithm-1
Step 1 ? Store the array elements.
Step 2 ? Use a for loop to traverse through all array elements.
Step 3 ? Compare all elements to find the smallest one.
Step 4 ? Use a foreach loop and print all elements except the smallest one.
Algorithm-2
Step 1 ? Store the array elements.
Step 2 ? Sort the array in ascending order.
Step 3 ? Run a for loop to traverse from 2nd element to end and print all elements.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length ?
Below refers to the syntax of it ?
array.length
Where ?array' refers to the array reference.
You can use the Arrays.sort() method to sort the array in ascending order.
Arrays.sort(array_name);
Multiple Approaches
We have provided the solution in different approaches.
Without Using Sorting
By Using Sorting
Let's see the program along with its output one by one.
Approach-1: Without Using Sorting
In this approach, we use a for loop to find the smallest element and then print all the elements except that.
Example
public class Main { public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, 99, 12, 10 }; System.out.println("The array elements are-"); // Print the array elements for (int i : arr) { System.out.print(i + ", "); } // Initialize the first element as smallest and compare int smallest = arr[0]; // Find the smallest element in the array for (int i = 0; i < arr.length; i++) if (arr[i] < smallest) smallest = arr[i]; // Print array elements that have at least one smaller element System.out.println("\nThe array elements that have at least one smaller element are-"); for (int i : arr) { if (i != smallest) System.out.print(i + ", "); } } }
Output
The array elements are- 10, 2, 3, 99, 12, 10, The array elements that have at least one smaller element are- 10, 3, 99, 12, 10,
Approach-2: By Using Sorting
In this approach, we sort the array using Arrays.sort() method and print all elements except the first one.
Example
import java.util.Arrays; public class Main { public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, 99, 12, 10 }; System.out.println("The array elements are-"); // Print the array elements for (int i : arr) { System.out.print(i + ", "); } // Sort the array Arrays.sort(arr); // Print the array elements from the 2nd element System.out.println("\nThe array elements that have at least one smallerelement are-"); for (int i = 1; i < arr.length; i++) { System.out.print(arr[i] + ", "); } } }
Output
The array elements are- 10, 2, 3, 99, 12, 10, The array elements that have at least one smallerelement are- 3, 10, 10, 12, 99,
In this article, we explored how to find all the array elements which have at least one smaller element by using Java programming language.