Skip to content
Related Articles

Related Articles

Finding Maximum Element of Java Vector
  • Last Updated : 28 Dec, 2020

Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. 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] ;

In both ways, we land up wasting memory so, in order to properly utilize memory optimization, Vectors were introduced. We have to find out the maximum element in the given Java vector.

Examples:

Input: [1,2,3,4,5]
Output: 5
Input: [88,23,76,90,56]
Output: 90

Approach 1: Using a Predefined Function

  • To find the minimum element of a given vector we use the java.util.Collections.max() method.
  • This directly finds the maximum value in the vector.

Java




// Java program to find
// maximum elemnent in java vector
  
import java.io.*;
import java.util.Collections;
import java.util.Vector;
public class GFG {
  
    public static void main(String args[])
  
        Vector<Integer> vec = new Vector<Integer>();
  
        vec.add(1);
        vec.add(2);
        vec.add(3);
        vec.add(4);
        vec.add(5);
  
        System.out.println("Vector elements: " + vec);
    
        System.out.println("The maximum element of the Vector is: "
         + Collections.max(vec));
    }
 }
Output



Vector elements: [1, 2, 3, 4, 5]
The maximum element of the Vector is: 5

Worst Case Time Complexity: O(n) where n is the number of elements present in the vector.

Approach 2: Comparing each element present in Vector

  • First, we will initialize a vector let’s say v, then we will store values in that vector.
  • Next, we will take a variable, let us say maxNumber and assign the maximum 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.

Java




// Java program to find largest element 
// present in Vector via comparison 
    
import java.io.*; 
    
// Importing Vector Class  
import java.util.Vector; 
    
// Importing Iterator Class 
import java.util.Iterator; 
    
class GFG { 
      
    // Main Method 
    public static void main(String[] args) 
    
        // initializing vector of Integer type 
        Vector<Integer> v = new Vector<Integer>(); 
          
        // Adding eleemnts in vector 
        v.add(10); 
        v.add(20); 
        v.add(30); 
        v.add(40); 
        v.add(50); 
          
        // Assigning min value possible 
        int maxValue = Integer.MIN_VALUE; 
          
        // Creating an iterator to traverse through vector 
        // in the begining itr will point to index just 
        // before first element 
        Iterator itr = v.iterator(); 
          
        // See if there is any next element 
        while (itr.hasNext())  
        
            // Moving iterator to next element 
            int element = (Integer)itr.next(); 
    
            // Comparing if element is larger than maxValue 
            if (element > maxValue)  
            
                // Update maxValue 
                maxValue = element; 
            
        
          
        // Print maxVaue 
        System.out.println( "The largest element present in Vector is : "
            + maxValue); 
    
}
Output
The largest element present in Vector is : 50

Time Complexity: O(n) where n is the number of elements present in the vector.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :