Skip to content
TechToolsHQ
NewsReviewsGuidesTech 101Tech SeriesToolsNewsletter
TechToolsHQ

Independent, research-driven tech coverage — breaking news, in-depth reviews, buying guides, and technical tutorials to help you understand and choose with confidence.

Explore
NewsReviewsGuidesTech 101Tech SeriesFree Tools
Legal
About UsOur AuthorsContact UsPrivacy PolicyTerms of ServiceCopyright & DMCAAffiliate DisclosureEditorial PolicyAdvertise With Us

© 2026 TechToolsHQ. All rights reserved.

Tech Series/Data Structures & Algorithms/5 Basic Array Problems in JavaScript: Largest, Second Largest, Smallest & Sorted Check
Data Structures & Algorithms · Part 2 of 2

5 Basic Array Problems in JavaScript: Largest, Second Largest, Smallest & Sorted Check

Find the largest, second largest, smallest and second smallest element, and check whether an array is sorted — each solved two ways.

By Himanshu Bhatt· 2 min read· October 7, 2026

Basic Array Problems — Data Structures & Algorithms series, Part 2
Key Takeaways · TL;DR
  • Five beginner array problems, solved in JavaScript.
  • Each problem has a brute-force solution and a better approach.
  • Part 2 of the Data Structures & Algorithms series.
☰ On this page(show)(close)
  • •Problem 1: Largest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Linear Iteration
  • •Problem 2: Second largest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Using Two Variables
  • •Problem 3: Smallest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Linear Iteration
  • •Problem 4: Second smallest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Using Two Variables
  • •Problem 5: Check if an array is sorted
  • ◦Brute Force: Nested Loops
  • ◦Better Approach: Compare Adjacent Elements
On this page
  • •Problem 1: Largest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Linear Iteration
  • •Problem 2: Second largest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Using Two Variables
  • •Problem 3: Smallest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Linear Iteration
  • •Problem 4: Second smallest element in an array
  • ◦Brute Force: Using Sorting
  • ◦Better Approach: Using Two Variables
  • •Problem 5: Check if an array is sorted
  • ◦Brute Force: Nested Loops
  • ◦Better Approach: Compare Adjacent Elements
INFO

This is Part 2 of the Data Structures & Algorithms series — five beginner array problems, each solved two ways: a brute-force solution and a better approach.

Problem 1: Largest element in an array

Given an array of N integers, find and return the largest element present in the array.

The array may contain positive numbers, negative numbers, or zero. Your task is to identify the maximum value among all elements.

Example 1
Input
5
12 45 7 89 23

Output
89

Explanation
Among the elements 12, 45, 7, 89, 23, the largest element is 89.


Example 2
Input
6
-5 -2 -10 -1 -8 -3

Output
-1

Explanation
Even though all elements are negative, -1 is the largest value in the array.

Brute Force: Using Sorting

function largestElement(nums){
  nums.sort((a, b) => a - b);

  return nums[nums.length - 1];
}

Better Approach: Linear Iteration

function largestElement(nums){
  let largest = nums[0];
  for(let i = 1; i<nums.length; i++){
    if(nums[i] > largest){
      largest = nums[i];
    }
  }
  return largest;
}

Problem 2: Second largest element in an array

Given an array of N integers, find and return the second largest distinct element present in the array.

If the array does not contain a second distinct largest element (for example, all elements are the same), return -1.

Example 1
Input
5
12 45 7 89 23

Output
45

Explanation
The largest element is 89, and the second largest distinct element is 45.


Example 2
Input
6
10 20 20 30 30 15

Output
20

Explanation
The distinct elements are 10, 15, 20, 30. The second largest among them is 20.


Example 3
Input
4
10 10 10 10

Output
-1

Explanation
There is no second largest distinct element because all elements are the same.

Brute Force: Using Sorting

function secondLargestElement(nums){
  nums.sort((a, b) => a - b);
  let largest = nums[nums.length - 1];

  for(let i = nums.length - 2; i>= 0; i--){
    if(nums[i] !== largest){
      return nums[i];
    }
  }
  return -1;
}

Better Approach: Using Two Variables

function secondLargestElement(nums){
  let largest = -Infinity;
  let secondLargest = -Infinity;

  for(let i = 0; i<nums.length; i++){
    if(nums[i] > largest){
      secondLargest = largest;
      largest = nums[i];
    }else if(nums[i] > secondLargest && nums[i] !== largest){
      secondLargest = nums[i];
    }
  }
  return (secondLargest === -Infinity) ? -1 : secondLargest;
}

Note: two fixes from the original — a leftover inner function secondLargestElement(nums){ line was removed (it left the function unclosed), and the final check now compares with -Infinity instead of Infinity, so an all-equal array returns -1.

Problem 3: Smallest element in an array

Given an array of N integers, find and return the smallest element present in the array.

The array may contain positive numbers, negative numbers, or zero. Your task is to identify the minimum value among all elements.

Example 1
Input
5
20 30 50 10 30

Output
10

Explanation
Among the elements 20, 30, 50, 10, 30, the smallest element is 10.


Example 2
Input
6
-5 -2 -10 -1 -8 -3

Output
-10

Explanation
Even though all elements are negative, -10 is the smallest value in the array.

Brute Force: Using Sorting

function smallestElement(nums){
  nums.sort((a, b) => a - b);

  return nums[0];
}

Better Approach: Linear Iteration

function smallestElement(nums){
  let smallest = nums[0];

  for(let i = 1; i<nums.length; i++){
    if(nums[i] < smallest){
      smallest = nums[i];
    }
  }
  return smallest;
}

Note: in the original, return nums[0]; sat outside the brute-force function and the linear version had no return smallest;. Both returns are now inside their functions.

Problem 4: Second smallest element in an array

Given an array of N integers, find and return the second smallest distinct element present in the array.

If the array does not contain a second distinct smallest element (for example, all elements are the same), return -1.

Example 1
Input
5
12 45 7 89 23

Output
12

Explanation
The smallest element is 7, and the second smallest distinct element is 12.


Example 2
Input
6
10 20 20 30 30 15

Output
15

Explanation
The distinct elements are 10, 15, 20, 30. The second smallest among them is 15.


Example 3
Input
4
10 10 10 10

Output
-1

Explanation
There is no second smallest distinct element because all elements are the same.

Brute Force: Using Sorting

function secondSmallest(nums){
  nums.sort((a, b) => a - b);
  let smallest = nums[0];

  for(let i = 1; i<nums.length; i++){
    if(nums[i] !== smallest){
      return nums[i];
    }
  }
  return -1;
}

Better Approach: Using Two Variables

function secondSmallest(nums){
  let smallest = Infinity;
  let secondSmallest = Infinity;

  for(let i = 0; i<nums.length; i++){
    if(nums[i] < smallest){
      secondSmallest = smallest;
      smallest = nums[i];
    }else if(nums[i] < secondSmallest && nums[i] !== smallest){
      secondSmallest = nums[i];
    }
  }

  return secondSmallest === Infinity ? -1 : secondSmallest;
}

Note: the original compared with > (nums[i] > smallest, nums[i] > secondSmallest). Nothing is greater than Infinity, so it always returned -1. Both comparisons are now <.

Problem 5: Check if an array is sorted

Given an array of N integers, determine whether the array is sorted in non-decreasing order (ascending order with duplicates allowed).

Return true if the array is sorted; otherwise, return false.

Example 1
Input
5
1 2 3 4 5

Output
true

Explanation
Every element is greater than or equal to the previous element.


Example 2
Input
5
1 2 5 4 6

Output
false

Explanation
The element 4 is smaller than 5, so the array is not sorted.

Brute Force: Nested Loops

function checkArrayIsSorted(nums){
  for(let i = 0; i<nums.length; i++){
    for(let j = i + 1; j<nums.length; j++){
      if(nums[j] < nums[i]){
        return false;
      }
    }
  }
  return true;
}

Better Approach: Compare Adjacent Elements

function checkArrayIsSorted(nums){
  for(let i = 1; i<nums.length; i++){
    if(nums[i-1] > nums[i]){
      return false;
    }
  }
  return true;
}
Share
Series: Data Structures & Algorithms

Part 2 — 5 Basic Array Problems in JavaScript: Largest, Second Largest, Smallest & Sorted Check

Part 2 of 2
← Previous partPart 1 — Pattern Printing Problems in JavaScript: 6 Star & Number Patterns Explained
Latest episode in this series.

Enjoying the Data Structures & Algorithms series?

TechToolsHQ is an independent, reader-supported tech platform. If this article saved you time, solved a tough problem, or helped you learn a new skill, consider supporting our work. Your support helps us keep our in-depth series 100% free and updated for everyone.

100% optional · Reader supported · Independent researchSupport our work

Don't miss the next deep-dive

Weekly breakdowns of the tools students and builders actually use.

No spam·Unsubscribe any time·Privacy-first