Minimum and Maximum Algorithms

# How do I find the second smallest or second largest element?

## How do I find the second smallest or second largest element using standard library algorithms?

Finding the second smallest or second largest element in a collection using the standard library algorithms involves a few additionalÂ steps.

Since std::ranges::min() and std::ranges::max() only return the smallest or largest elements, you need to remove these elements and then find the next smallest orÂ largest.

### Finding the Second Smallest Element

Hereâ€™s how you can find the second smallest element in aÂ vector:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers{5, 1, 4, 2, 3};

// Find the smallest element
auto minIt = std::ranges::min_element(numbers);
int minElement = *minIt;

// Remove the smallest element
numbers.erase(minIt);

// Find the second smallest element
int secondMinElement = std::ranges::min(numbers);

std::cout << "Smallest element: "
<< minElement << "\n";
std::cout << "Second smallest element: "
<< secondMinElement;
}
Smallest element: 1
Second smallest element: 2

### Finding the Second Largest Element

Similarly, to find the second largest element, you can follow theseÂ steps:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers{5, 1, 4, 2, 3};

// Find the largest element
auto maxIt = std::ranges::max_element(numbers);
int maxElement = *maxIt;

// Remove the largest element
numbers.erase(maxIt);

// Find the second largest element
int secondMaxElement = std::ranges::max(numbers);

std::cout << "Largest element: "
<< maxElement << "\n";
std::cout << "Second largest element: "
<< secondMaxElement;
}
Largest element: 5
Second largest element: 4

### Explanation

1. Find the Min/Max: Use std::ranges::min_element() or std::ranges::max_element() to find the smallest or largest element.
2. Remove the Element: Use erase() to remove the found element from the collection.
3. Find the Second Min/Max: Use std::ranges::min() or std::ranges::max() to find the next smallest or largest element.

### Important Considerations

• Mutating the Collection: Removing elements mutates the original collection. If you need to preserve the collection, consider working on a copy.
• Performance: Removing elements can be costly in terms of performance for large collections. If performance is critical, consider alternative approaches like sorting.

By following these steps, you can effectively find the second smallest or second largest element in a collection using C++ standard libraryÂ algorithms.

This Question is from the Lesson:

### Minimum and Maximum Algorithms

An introduction to the seven minimum and maximum algorithms in the C++ standard library: clamp(), min(), min_element(), max(), max_element(), minmax(), and minmax_element().

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

### Minimum and Maximum Algorithms

An introduction to the seven minimum and maximum algorithms in the C++ standard library: clamp(), min(), min_element(), max(), max_element(), minmax(), and minmax_element().

Part of the course:

## Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

### This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

### Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.