const array = [320, 52, 532, 920, 20];
let smallestNum = array[0];
for (let i = 1; i < array.length; i++) {
if (array[i] < smallestNum) {
smallestNum = array[i];
}
}
console.log(smallestNum);
const array = [32, 22, 53, 92, 20, 34, 23, 11, 17];
let smallestNum = array[0];
let secondSmallestNum = 0;
for (let i = 1; i < array.length; i++) {
if (array[i] < smallestNum) {
secondSmallestNum = smallestNum;
smallestNum = array[i];
} else if (array[i] !== smallestNum && array[i] < secondSmallestNum) {
secondSmallestNum = array[i];
}
}
console.log(smallestNum);
console.log(secondSmallestNum);
#include <iostream>
#include<vector>
using namespace std;
int main() {
vector<int> v;
int n;
cout<<"Enter total element: ";
cin>>n;
// Initializing array
cout<<"Enter values: "<<endl;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
v.push_back(temp);
}
// Finding second smallest using single loop
int smallest=v[0];
int secondSmallest=v[1];
for(int i=0;i<n;i++){
if(v[i]<smallest){
secondSmallest=smallest;
smallest=v[i];
}
if(v[i]<secondSmallest && v[i]!=smallest){
secondSmallest=v[i];
}
}
cout<<"Smallest Number: "<<smallest<<endl;
cout<<"Second Smallest Number: "<<secondSmallest<<endl;
return 0;
}