
- 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
Find Array Elements Which has 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.
- Related Articles
- Find all elements in array which have at-least two greater elements in C++
- Count elements that are divisible by at-least one element in another array in C++
- Print array elements that are divisible by at-least one other in C++
- Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python
- Count pairs in an array such that at least one element is prime in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find Number of Array Elements Smaller than a Given Number in Java
- Find if sum of odd or even array elements are smaller in Java
- Program to find elements from list which have occurred at least k times in Python
- How to check if a string has at least one letter and one number in Python?
- Find the least frequent element in an array using Python
- How to aggregate two lists if at least one element matches in MongoDB?
- Count all elements in the array which appears at least K times after their first occurrence in C++
- Find closest smaller value for every element in array in C++
- Find Array Elements Which are Greater than its Left Elements in Java?
