Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find smallest number in array javascript using for loop

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

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

how to find second smallest element in an array 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
Javascript :: scroll js 
Javascript :: usenavigate and uselocation in react 
Javascript :: convert int to string javascript 
Javascript :: is displayed 
Javascript :: mock createRef jest react functional component 
Javascript :: how to assign onEdit to specigfic tab 
Javascript :: how to mark a icon with vector icons in mapview 
Javascript :: how to implement certain actions after setstate in react hooks 
Javascript :: jquery append text for 5 seconds 
Javascript :: javascript How to print every number that is divisible by either 3 or 5, but not both 
Javascript :: odd even condition 
Javascript :: querySelectorAll multiple lists 
Javascript :: table to pdf javascript 
Javascript :: Material-ui wallet icon 
Javascript :: find the height of space above element using javascript 
Javascript :: js insert a point each three digit 
Javascript :: import json file into javascript 
Javascript :: JavaScript alert massage prompt 
Javascript :: export default module 
Javascript :: node redirect 
Javascript :: e editable select no button 
Javascript :: if in javascript 
Javascript :: scrape html table javascript 
Javascript :: html2canvas not getting image if file field src is url 
Javascript :: node js download image from url as buffer 
Javascript :: Delete a Cookie with JavaScript 
Javascript :: Getting Search Parameters From A URL As An Array 
Javascript :: js hide element 
Javascript :: agrgar atributo con id jquey 
Javascript :: separador de miles javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =