Finding the largest and smallest number in an unsorted array of integers in JavaScript

In the given problem statement we have to find the largest and smallest number in an unsorted array of integers with the help of Javascript functionalities. So we will use an array of integers and find the smallest and largest number from the array.

Understanding the Problem

The problem we have is to find the largest and smallest values in an unsorted array of integers. So in this task we will see a solution in Javascript. With the help of it we can determine the largest and smallest numbers in an array without sorting the array. For example suppose we have an unsorted array as [11, 21, 14, 32, 20, 12] so in this array the smallest number is 11 and the largest number is 32.

Logic for the given Problem

To solve the given problem of finding the smallest and largest number from an unsorted array, we will use a simple linear scan technique. So first we will initialize two variables to store the smallest and largest numbers of the array. Then we will iterate the remaining items and compare every item with the current smallest and largest values. And if we found the values then we will update in the respective variables. At the end of the iteration process we will have the values of smallest and largest elements of the array without sorting the array.

Algorithm

Step 1: As our task is to find the smallest and highest values from the given input array without sorting the array. So we will define a function called smallestAndLargest and in this function we will pass a parameter array.

Step 2: So we have already created the function and the input array. Now we will define a condition to check the length of the array is empty in that case we will return null.

Step 3: After the above conditions we will declare two variables to store the values of smallest and largest numbers from the array. And give them names as smallest and largest. Initialize these variables with the first item of the array.

Step 4: Now we will use a loop to iterate the numbers of the array and check the conditions if the current item is smaller than smallest number then we will update the value of smallest to the current item.

Step 5: And we will also check the condition for the greatest number. If the current item is larger than the largest number then we will update the value of largest with the current item.

Step 6: At the end we will have the smallest and largest numbers of the array so return these values.

Example

// Function to find the smallest and largest number
function smallestAndLargest(arr) {
if (arr.length === 0) {
return null; // Empty array case
}

let smallest = arr[0];
let largest = arr[0];

for (let i = 1; i < arr.length; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
} else if (arr[i] > largest) {
largest = arr[i];
}
}

return { smallest, largest };
}

const nums = [4, 2, 9, 1, 7, 5];
const result = smallestAndLargest(nums);
console.log("Smallest:", result.smallest);
console.log("Largest:", result.largest);


Output

Smallest: 1
Largest: 9


Complexity

The time complexity for finding the smallest and largest values of the array without sorting the array is O(n), in which n is the size of the given array. As we have iterated over every item once to compare it with the current smallest and largest values. So the time taken to get these values is linear with the size of the input. The space complexity for the code is O(1) because we have used two variables to store the values of smallest and largest items.

Conclusion

In this solution we have learned about finding the smallest and largest items of the array without sorting the array. We have used very basic functionalities of Javascript to get the desired results with linear time complexity.

Updated on: 14-Aug-2023

4K+ Views