Second Largest Element in Array | Brute Force Method Explained 🚀

Опубликовано: 04 Август 2026
на канале: Code & Test
6
1

In this video, we’ll learn how to find the Second Largest Element in an array using a simple Brute Force (Sorting) approach.

We will go step by step and understand:

How sorting helps in solving the problem
How to handle duplicate elements
What to do when the second largest element does not exist
Edge cases like all elements being the same
🧠 Problem Statement

Given an array of integers nums, return the second-largest element.
If it does not exist, return -1.

🔍 Approach (Brute Force - Sorting)
Sort the array in ascending order
The largest element will be at the last index
Traverse backward to find the first element smaller than the largest
That element is your second largest
⚠️ Edge Cases Covered
Array has less than 2 elements → return -1
All elements are same → return -1
⏱️ Complexity
Time Complexity: O(N log N)
Space Complexity: O(1)
📌 Examples

Input: [8, 8, 7, 6, 5] → Output: 7
Input: [10, 10, 10, 10] → Output: -1

💡 This is a beginner-friendly DSA problem and a great starting point for mastering arrays!

👉 Don’t forget to Like, Share, and Subscribe for more DSA content!

🏷️ Tags

second largest element
second largest in array
second largest element java
find second largest number
array problems java
dsa for beginners
sorting approach array
brute force algorithm
java dsa
coding interview questions
arrays interview questions
data structures and algorithms
easy dsa problems
placement preparation
coding practice