“Encuentre el segundo número más pequeño en la matriz JavaScript usando para bucle” Código de respuesta

Encuentre el segundo número más grande en la matriz JavaScript usando para bucle

const array = [32, 523, 5632, 920, 6000];

let largestNum = array[0];
let secondLargestNum = 0;

for(let i = 1; i < array.length; i++) {
	if(array[i] > largestNum) {
    secondLargestNum = largestNum;
    largestNum = array[i];  
    }
  if else (array[i] !== largestNum && array[i] > secondLargestNum) {
  secondLargestNum = array[i];
  }
};
console.log("Largest Number in the array is " + largestNum);
console.log("Second Largest Number in the array is " + secondLargestNum);

/* Explanation: 
1) Initialize the largestNum as index of arr[0] element
2) Start traversing the array from array[1],
   a) If the current element in array say arr[i] is greater
      than largestNum. Then update largestNum and secondLargestNum as,
      secondLargestNum = largestNum
      largestNum = arr[i]
   b) If the current element is in between largestNum and secondLargestNum,
      then update secondLargestNum to store the value of current variable as
      secondLargestNum = arr[i]
3) Return the value stored in secondLargestNum.
*/
Md. Ashikur Rahman

Encuentre el segundo número más pequeño en la matriz JavaScript usando para bucle

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);
Md. Ashikur Rahman

Encuentre el número más pequeño en la matriz JavaScript usando para bucle

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);
Md. Ashikur Rahman

Segundo elemento más pequeño en la matriz usando un bucle


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


Abhay Singh

Respuestas similares a “Encuentre el segundo número más pequeño en la matriz JavaScript usando para bucle”

Preguntas similares a “Encuentre el segundo número más pequeño en la matriz JavaScript usando para bucle”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código