To find the smallest element in an array, iterate through the array and keep track of the smallest element.
- Initialize a variable to store the smallest element with a large value (e.g.,
Infinity
). - Iterate through the array and compare each element to the current smallest value.
- Update the smallest value whenever a smaller element is found.
- Return the smallest value.
function findSmallest(arr) {
let smallest = Infinity;
for (const num of arr) {
if (num < smallest) {
smallest = num;
}
}
return smallest;
}
// Example usage
console.log(findSmallest([5, 3, 8, 1, 6])); // Output: 1
console.log(findSmallest([7, 4, 9, 2])); // Output: 2
This method has a time complexity of O(n), where n is the length of the array.
Tags: basic, JavaScript, Arrays, Algorithm