Open In App

Finding Minimum Element of Java Vector

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 need to find out the minimum element in the given java vector.

Examples:



Input: [1,2,3,4,5]
Output: 1

Input: [88,23,76,90,56]
Output: 23

Approach 1: Using a Predefined Function




// Java program to find
// minimum element 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.min(vec));
    }
}

 
 

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

 

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

 

 




// Java program to find minimum element
// present in Vector via comparison
  
import java.io.*;
// Importing Iterator Class
import java.util.Iterator;
// Importing Vector Class
import java.util.Vector;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
    {
        // initializing vector of Integer type
        Vector<Integer> v = new Vector<Integer>();
  
        // Adding elements in vector
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
  
        // Assigning max value possible
        int minValue = Integer.MAX_VALUE;
  
        // Creating an iterator to traverse through vector
        // in the beginning 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 smaller than minValue
            if (element < minValue) 
            {
                // Update maxValue
                minValue = element;
            }
        }
  
        // Print minVaue
        System.out.println("The smallest element present in Vector is : "
            + minValue);
    }
}

 
 

Output
The smallest element present in Vector is : 10

 

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

 


Article Tags :