Java Program to Get the Maximum Element From a Vector
Last Updated :
07 Jul, 2021
Prerequisite: Vectors in Java
Why We Use Vector?
Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory.
int Array_name[Fixed_size] ;
int array_name[variable_size] ;
Both ways we land up wasting memory so in order to properly utilize memory optimization vectors are introduced.
Advantages Of Using Vectors
- Dynamic Size
- Rich Library Functions
- Easy To Know Size
- No Need To Pass Size
- Can be returned from a function
- By default initializes with default values
Rich Library Functions Includes
- Find An Element
- Erase An Element
- Insert An Element
Here, we use rich library functions to get a maximum element.
Note: Arrays are always passed as a pointer in arrays so another parameter you must pass the size of the array but that is not required in the case of vectors.
Syntax :
In the case of Arrays
type function_Name(type arrayName[], type sizeOfArray) ;
In the case of Vectors
type function_Name(vector<type> vectorName) ;
Considering An Example given a vector, the task is to find the maximum element.
Examples:
Input: v1={1,2,3,4,5}
Output: 5
Input: v2={7,50,0,67,98}
Output: 98
Method 1: Using a Predefined Function
- First, we will initialize a vector lets say v, then we will store values in that vector.
- After that, we will call the predefined method called max() defined in class java.util.Collections.
- Print the max element.
Below is the implementation of the above approach.
Java
import java.io.*;
import java.util.Collections;
import java.util.Vector;
class GFG {
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
v.add( 7 );
v.add( 50 );
v.add( 0 );
v.add( 67 );
v.add( 98 );
int n = Collections.max(v);
System.out.println(
"The maximum value present in Vector is : "
+ n);
}
}
|
Output :
The maximum value present in Vector is : 98
Worst Case Time Complexity: O(n) where n is the number of elements present in the vector.
Method 2: Comparing each element present in Vector
- First, we will initialize a vector lets say v, then we will store values in that vector.
- Next, we will take a variable, let us say maxNumber and assign the minimum value possible.
- Traverse till the end of vector and compare each element of a vector with maxNumber.
- If the element present in the vector is greater than maxNumber, then update maxNumber to that value.
- Print maxNumber.
Below is the implementation of the above approach.
Java
import java.io.*;
import java.util.Vector;
import java.util.Iterator;
class GFG {
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
v.add( 1 );
v.add( 2 );
v.add( 3 );
v.add( 4 );
v.add( 5 );
int maxValue = Integer.MIN_VALUE;
Iterator itr = v.iterator();
while (itr.hasNext())
{
int element = (Integer)itr.next();
if (element > maxValue)
{
maxValue = element;
}
}
System.out.println(
"The largest element present in Vector is : "
+ maxValue);
}
}
|
Output :
The largest element present in Vector is : 5
Time Complexity: O(n) where n is the number of elements present in the vector.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...