Search
 
SCRIPT & CODE EXAMPLE
 

CPP

find second smallest number in array javascript using for loop

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);
Comment

second smallest element using single loop


#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;
}


Comment

PREVIOUS NEXT
Code Example
Cpp :: txt to pdf CPP 
Cpp :: I/O Redirection in C++ 
Cpp :: turn it codechef solution in c++ 
Cpp :: ordine crescente di numeri indefiniti in c++ 
Cpp :: array di struct 
Cpp :: how to type cast quotient of two integers to double with c++ 
Cpp :: Check whether K-th bit is set or not c++ 
Cpp :: .txt file into .cpp 
Cpp :: Print value of data in c++ 
Cpp :: como copiar codigo de c++ con numeros de fila en docs 
Cpp :: how to use run total in C++ 
Cpp :: product of array in cpp 
Cpp :: heroatx77 
Cpp :: solve diamond inheritance c++ 
Cpp :: statement that causes a function to end in c++ 
Cpp :: stack using cpp 
Cpp :: hpp files 
Cpp :: add two constant char pointers c++ 
Cpp :: Snake Procession codechef solution in c++ 
Cpp :: print the elements of the array without using the [] notation in c++ 
Cpp :: all in one c++ 
Cpp :: cpp reference array 
Cpp :: catalan numbers c++ 
Cpp :: Remove the jth object from the subset 
Cpp :: the partition function of a system is given by z= 1/(1-e^-bEi), calculate the total energy of the system 
Cpp :: 5 program code in c++ of friend function 
Cpp :: inverse elment of array c++ 
Cpp :: permutation and combination program in c++ 
Cpp :: c++ how to print out 
Cpp :: amusia 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =