Tìm số nhỏ nhất trong mảng javascript cho vòng lặp

Có nhiều phương pháp để tìm các số nhỏ nhất và lớn nhất trong một mảng JavaScript và hiệu suất của các phương pháp này thay đổi dựa trên số lượng phần tử trong mảng. Hãy thảo luận riêng về từng vấn đề và đưa ra kết quả thử nghiệm cuối cùng

Đối với các mảng thông thường, bạn có thể sử dụng Math. tối đa với ba dấu chấm

let max = Math.max(...arrayOfNumbers);

Tìm số nhỏ nhất trong mảng javascript cho vòng lặp
Toán Javascript. max tìm max trong mảng

hãy để arrayOfNumbers = [4, 12, 62, 70, -10]; . nhật ký (Toán. tối đa (. arrayOfNumbers));

Sử dụng dấu ba chấm (…) giúp dễ dàng gọi bất kỳ hàm nào cần đối số

Toán học. hàm max sử dụng phương thức apply() để tìm phần tử lớn nhất trong một mảng số

Math.min.apply(Math, testArr);
Math.max.apply(Math, testArr);

Tìm số nhỏ nhất trong mảng javascript cho vòng lặp
Toán Javascript. tối đa. áp dụng tìm phần tử max trong mảng

hãy để arrayOfNumbers = [4, 12, 62, 70, -10]; . nhật ký (Toán. tối đa. áp dụng (Math, arrayOfNumbers));

Toán tử cũng được sử dụng để lấy giá trị lớn nhất của một mảng. Nó mở rộng một mảng số vào danh sách các đối số, chẳng hạn như với Math. tối thiểu () và Toán. tối đa()

Math.min(...testArr);
Math.max(...testArr);

Math.min.apply(Math, testArr);
Math.max.apply(Math, testArr);

Bạn có thể sử dụng vòng lặp tiêu chuẩn cho hàng tấn đối số vì vòng lặp for không có giới hạn về kích thước

let max = testArray[0];
for (let i = 1; i < testArrayLength; ++i) {
  if (testArray[i] > max) {
    max = testArray[i];
  }
}

let min = testArray[0];
for (let i = 1; i < testArrayLength; ++i) {
  if (testArray[i] < min) {
    min = testArray[i];
  }
}

Tìm số nhỏ nhất trong mảng javascript cho vòng lặp
Javascript vòng lặp tiêu chuẩn tìm phần tử tối đa trong mảng

cho arrayList = [1, 2, 3, 4, 3, 21, 0]; . chiều dài; . nhật ký (tối đa);

Bạn cũng có thể sử dụng phương thức reduce() để lấy số lượng mục

testArr.reduce(function (a, b) {
  return Math.max(a, b);
});
testArr.reduce(function (a, b) {
  return Math.min(a, b);
});

Tìm số nhỏ nhất trong mảng javascript cho vòng lặp
Phương thức Javascript reduce() để lấy số mục tối đa ar tối thiểu

hãy để mảngList = [1, 2, 3, 4, 3, 20, 0]; . giảm ((trước, hiện tại) => { trả về Toán học. max(trước, hiện tại) }); . nhật ký (maxNum);

Các phương thức áp dụng và trải rộng có giới hạn là 65536 xuất phát từ giới hạn số lượng đối số tối đa. Vào năm 2019, giới hạn là kích thước tối đa của ngăn xếp cuộc gọi, nghĩa là kích thước tối đa cho các số trong trường hợp áp dụng và giải pháp trải rộng là khoảng 120000. Tập lệnh sau sẽ tính toán giới hạn cho môi trường cụ thể của bạn

let testArr = Array.from({
  length: 10000
}, () => Math.floor(Math.random() * 2000000));
for (i = 10000; i < 1000000; ++i) {
  testArr.push(Math.floor(Math.random() * 2000000));
  try {
    Math.max.apply(null, testArr);
  } catch (e) {
    console.log(i);
    break;
  }
}

Khi bạn kiểm tra tất cả các ví dụ đã cho ở trên, kết quả cho thấy vòng lặp tiêu chuẩn là nhanh nhất. Sau đó đến các phương pháp áp dụng và lây lan, sau chúng là giảm, đây là phương pháp chậm nhất. When dealing with large arrays of more than 40 elements, the spread operator is considered a worse choice compared to other methods

A Simple Solution is to sort the array in increasing order. The first two elements in the sorted array would be the two smallest elements. In this approach, if the smallest element is present more than one time then we will have to use a loop for printing the unique smallest and second smallest elements.  

Below is the implementation of the above approach

C++




//C++ simple approach to print smallest

//and second smallest element.

#include

using

smallest element is 1
second smallest element is 9
0
smallest element is 1
second smallest element is 9
1

smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
3

smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
5

smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
7_______6_______8
smallest element is 1
second smallest element is 9
9
smallest element is 1
second smallest element is 9
8
smallest element is: 1
second smallest element is: 10
1

smallest element is: 1
second smallest element is: 10
2

smallest element is: 1
second smallest element is: 10
3

smallest element is: 1
second smallest element is: 10
4

smallest element is: 1
second smallest element is: 10
5

smallest element is: 1
second smallest element is: 10
6_______17_______7
smallest element is: 1
second smallest element is: 10
8

smallest element is: 1
second smallest element is: 10
6_______26_______0
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
1

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
3

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
5

Java




1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
6

 

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
7
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
8

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
7
The smallest element is 1 and second Smallest element is 10
0

 

The smallest element is 1 and second Smallest element is 10
1
The smallest element is 1 and second Smallest element is 10
2

The smallest element is 1 and second Smallest element is 10
3

//and second smallest element.

 

The smallest element is 1 and second Smallest element is 10
5

The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
7
The smallest element is 1 and second Smallest element is 10
8
The smallest element is 1 and second Smallest element is 10
9

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
smallest element : 1
second smallest element : 2
3
smallest element : 1
second smallest element : 2
4
smallest element : 1
second smallest element : 2
5
smallest element : 1
second smallest element : 2
6
smallest element : 1
second smallest element : 2
5
smallest element : 1
second smallest element : 2
8
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest0
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest2
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4//C++ simple approach to print smallest5

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2 //C++ simple approach to print smallest8

//C++ simple approach to print smallest9

smallest element : 1
second smallest element : 2
1//and second smallest element.1

smallest element : 1
second smallest element : 2
1//and second smallest element.3

smallest element : 1
second smallest element : 2
1//and second smallest element.5

//C++ simple approach to print smallest9

smallest element : 1
second smallest element : 2
1//and second smallest element.8

smallest element : 1
second smallest element : 2
1#include0
smallest element is: 1
second smallest element is: 10
7#include2#include3#include4

smallest element : 1
second smallest element : 2
1#include0
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
0#include2//C++ simple approach to print smallest4#include4

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

using3

Python3




using4

using5

 

using6

 

using7using8 using9

smallest element : 1
second smallest element : 2
4
smallest element : 1
second smallest element : 2
5
smallest element : 1
second smallest element : 2
6
smallest element : 1
second smallest element : 2
5_______45_______8
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest0
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest2
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4_______6_______11

smallest element is 1
second smallest element is 9
12_______1194_______8
smallest element is 1
second smallest element is 9
14
smallest element is 1
second smallest element is 9
15

smallest element is 1
second smallest element is 9
16

smallest element is 1
second smallest element is 9
17

smallest element is 1
second smallest element is 9
18

 

smallest element is 1
second smallest element is 9
19

smallest element is 1
second smallest element is 9
20
smallest element is 1
second smallest element is 9
21
smallest element is: 1
second smallest element is: 10
7
smallest element is 1
second smallest element is 9
23
smallest element is 1
second smallest element is 9
24
smallest element is 1
second smallest element is 9
25#include3
smallest element is 1
second smallest element is 9
27

smallest element is 1
second smallest element is 9
20
smallest element is 1
second smallest element is 9
21
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
0
smallest element is 1
second smallest element is 9
23
smallest element is 1
second smallest element is 9
24
smallest element is 1
second smallest element is 9
25//C++ simple approach to print smallest4
smallest element is 1
second smallest element is 9
27

 

smallest element is 1
second smallest element is 9
36

C#




smallest element is 1
second smallest element is 9
37

smallest element is 1
second smallest element is 9
38

using

smallest element is 1
second smallest element is 9
40

 

The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
1
smallest element is 1
second smallest element is 9
43

smallest element : 1
second smallest element : 2
0

//C++ simple approach to print smallest9____36_______5

//C++ simple approach to print smallest9_______36_______7

The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
8
smallest element is 1
second smallest element is 9
51

//C++ simple approach to print smallest9____45_______0

smallest element : 1
second smallest element : 2
1_______6_______2
smallest element is 1
second smallest element is 9
56

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
59

 

smallest element : 1
second smallest element : 2
1//and second smallest element.1

smallest element : 1
second smallest element : 2
1//and second smallest element.3

smallest element : 1
second smallest element : 2
1_______6_______65

 

smallest element : 1
second smallest element : 2
1//and second smallest element.8

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
69
smallest element is: 1
second smallest element is: 10
7
smallest element is 1
second smallest element is 9
71

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
69
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
0
smallest element is 1
second smallest element is 9
75

//C++ simple approach to print smallest9____26_______4

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

smallest element is 1
second smallest element is 9
79

Javascript




smallest element is 1
second smallest element is 9
80

 

smallest element is 1
second smallest element is 9
81

smallest element is 1
second smallest element is 9
38

 

smallest element is 1
second smallest element is 9
83

 

smallest element is 1
second smallest element is 9
84

smallest element is 1
second smallest element is 9
85

//and second smallest element.1

//and second smallest element.3

smallest element is 1
second smallest element is 9
88

 

//and second smallest element.8

smallest element is 1
second smallest element is 9
90
smallest element is: 1
second smallest element is: 10
7
smallest element is 1
second smallest element is 9
92
smallest element is 1
second smallest element is 9
93
smallest element is 1
second smallest element is 9
94

smallest element is 1
second smallest element is 9
90
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
0
smallest element is 1
second smallest element is 9
97
smallest element is 1
second smallest element is 9
93
smallest element is 1
second smallest element is 9
94

 

using3

 

smallest element is: 1
second smallest element is: 10
01

Đầu ra

smallest element is 1
second smallest element is 9

Độ phức tạp về thời gian. O(N*logN)
không gian phụ trợ. Ô(1)

Một cách tiếp cận hiệu quả. Giải pháp tốt hơn là quét mảng hai lần. Trong lần duyệt đầu tiên, hãy tìm phần tử tối thiểu. Đặt phần tử này là x. Trong lần duyệt thứ hai, tìm phần tử nhỏ nhất lớn hơn x

Sử dụng phương pháp này, chúng ta có thể khắc phục sự cố của Phương pháp 1 xảy ra khi phần tử nhỏ nhất có mặt trong một mảng nhiều lần
Giải pháp trên yêu cầu hai lần duyệt mảng đầu vào.  

C++




smallest element is: 1
second smallest element is: 10
02

smallest element is: 1
second smallest element is: 10
03

smallest element is: 1
second smallest element is: 10
04

using

smallest element is 1
second smallest element is 9
0
smallest element is 1
second smallest element is 9
1

smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
09

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1____6_______2
smallest element is: 1
second smallest element is: 10
13

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
16
smallest element is 1
second smallest element is 9
8
smallest element is: 1
second smallest element is: 10
18
smallest element is 1
second smallest element is 9
8
smallest element is: 1
second smallest element is: 10
1

smallest element : 1
second smallest element : 2
1____6_______2
smallest element is: 1
second smallest element is: 10
23

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
25

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
27

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
32

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______17_______36
smallest element is: 1
second smallest element is: 10
37

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40
smallest element is: 1
second smallest element is: 10
41

smallest element is: 1
second smallest element is: 10
35_______26_______4

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
47
smallest element is: 1
second smallest element is: 10
48
smallest element is: 1
second smallest element is: 10
49

 

smallest element : 1
second smallest element : 2
1____6_______2
smallest element is: 1
second smallest element is: 10
52

 

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
54

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
32

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______17_______36
smallest element is: 1
second smallest element is: 10
64

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40
smallest element is: 1
second smallest element is: 10
68

smallest element is: 1
second smallest element is: 10
35_______26_______4

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
47
smallest element is: 1
second smallest element is: 10
75
smallest element is: 1
second smallest element is: 10
76

smallest element : 1
second smallest element : 2
1____26_______2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
3

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

smallest element is: 1
second smallest element is: 10
81

Java




smallest element is: 1
second smallest element is: 10
82

smallest element is: 1
second smallest element is: 10
03

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
7
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
8

The smallest element is 1 and second Smallest element is 10
1
The smallest element is 1 and second Smallest element is 10
2

smallest element : 1
second smallest element : 2
1
The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
7
The smallest element is 1 and second Smallest element is 10
8
The smallest element is 1 and second Smallest element is 10
9

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______6_______2
smallest element is: 1
second smallest element is: 10
97
smallest element is: 1
second smallest element is: 10
98
smallest element : 1
second smallest element : 2
5
smallest element : 1
second smallest element : 2
6
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4
smallest element : 1
second smallest element : 2
5
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
04
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest2
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4 //C++ simple approach to print smallest5

smallest element is: 1
second smallest element is: 10
35_______6_______2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
12

smallest element is: 1
second smallest element is: 10
35_______6_______2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
15

smallest element is: 1
second smallest element is: 10
35_______17_______25

smallest element is: 1
second smallest element is: 10
35_______17_______27

smallest element is: 1
second smallest element is: 10
35_______17_______29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
24#include3
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
26

smallest element is: 1
second smallest element is: 10
40
smallest element is: 1
second smallest element is: 10
36
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
29

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30_______17_______41

smallest element is: 1
second smallest element is: 10
40
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

smallest element is: 1
second smallest element is: 10
35_______26_______4

smallest element is: 1
second smallest element is: 10
35_______1193_______0
smallest element is: 1
second smallest element is: 10
48

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
39_______26_______40

 

smallest element is: 1
second smallest element is: 10
35_______6_______2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
43

 

smallest element is: 1
second smallest element is: 10
35_______26_______45

smallest element is: 1
second smallest element is: 10
35_______26_______47

smallest element is: 1
second smallest element is: 10
35_______17_______29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
24#include3
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
26

smallest element is: 1
second smallest element is: 10
40
smallest element is: 1
second smallest element is: 10
36
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
57

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30_______26_______59

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30_______17_______68

smallest element is: 1
second smallest element is: 10
40
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

smallest element is: 1
second smallest element is: 10
35_______26_______4

smallest element is: 1
second smallest element is: 10
35_______1193_______0
smallest element is: 1
second smallest element is: 10
75

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
39_______26_______70

smallest element : 1
second smallest element : 2
1____26_______4

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
74

con trăn




1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
75

 

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
76

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
7
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
78

 

using7using8 using9

smallest element is: 1
second smallest element is: 10
98
smallest element : 1
second smallest element : 2
5
smallest element : 1
second smallest element : 2
6
smallest element : 1
second smallest element : 2
5_______1191_______4
smallest element : 1
second smallest element : 2
5
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
04
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest2
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4_______6_______11

smallest element is 1
second smallest element is 9
12_______1194_______8
smallest element is 1
second smallest element is 9
14
smallest element is 1
second smallest element is 9
15

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
98using8
The smallest element is 1 and second Smallest element is 10
00

 

The smallest element is 1 and second Smallest element is 10
01

smallest element is: 1
second smallest element is: 10
29
The smallest element is 1 and second Smallest element is 10
03
The smallest element is 1 and second Smallest element is 10
04
The smallest element is 1 and second Smallest element is 10
05
The smallest element is 1 and second Smallest element is 10
06

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
The smallest element is 1 and second Smallest element is 10
09

smallest element is: 1
second smallest element is: 10
35_______26_______98using8
The smallest element is 1 and second Smallest element is 10
13

 

smallest element is 1
second smallest element is 9
20
smallest element is 1
second smallest element is 9
21
The smallest element is 1 and second Smallest element is 10
16
smallest element is 1
second smallest element is 9
23
smallest element is 1
second smallest element is 9
24
The smallest element is 1 and second Smallest element is 10
19

The smallest element is 1 and second Smallest element is 10
20_______1194_______8
The smallest element is 1 and second Smallest element is 10
00

 

The smallest element is 1 and second Smallest element is 10
23

smallest element is: 1
second smallest element is: 10
29
The smallest element is 1 and second Smallest element is 10
03
The smallest element is 1 and second Smallest element is 10
04
The smallest element is 1 and second Smallest element is 10
05
The smallest element is 1 and second Smallest element is 10
06

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
57
The smallest element is 1 and second Smallest element is 10
32
The smallest element is 1 and second Smallest element is 10
33

smallest element is: 1
second smallest element is: 10
35_______36_______20using8
The smallest element is 1 and second Smallest element is 10
13

 

smallest element is 1
second smallest element is 9
20
smallest element is 1
second smallest element is 9
21
The smallest element is 1 and second Smallest element is 10
40
smallest element is 1
second smallest element is 9
23
smallest element is 1
second smallest element is 9
24
The smallest element is 1 and second Smallest element is 10
43

 

The smallest element is 1 and second Smallest element is 10
44

C#




The smallest element is 1 and second Smallest element is 10
45

smallest element is: 1
second smallest element is: 10
03

 

using

smallest element is 1
second smallest element is 9
40

 

The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
1
smallest element is 1
second smallest element is 9
43

smallest element : 1
second smallest element : 2
0

//C++ simple approach to print smallest9_______36_______7

The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
8
The smallest element is 1 and second Smallest element is 10
57

//C++ simple approach to print smallest9____45_______0

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
The smallest element is 1 and second Smallest element is 10
62

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
59

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
The smallest element is 1 and second Smallest element is 10
68

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
25

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
27

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
32

smallest element : 1
second smallest element : 2
1____45_______0

The smallest element is 1 and second Smallest element is 10
80
smallest element is: 1
second smallest element is: 10
36
smallest element is: 1
second smallest element is: 10
37

The smallest element is 1 and second Smallest element is 10
80_______45_______0

smallest element is: 1
second smallest element is: 10
35_______17_______41

The smallest element is 1 and second Smallest element is 10
80
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
69
smallest element is: 1
second smallest element is: 10
48
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
40

 

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
The smallest element is 1 and second Smallest element is 10
97

 

smallest element : 1
second smallest element : 2
1_______26_______45

smallest element : 1
second smallest element : 2
1____26_______47

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
32

smallest element : 1
second smallest element : 2
1____45_______0

The smallest element is 1 and second Smallest element is 10
80
smallest element is: 1
second smallest element is: 10
36
smallest element is: 1
second smallest element is: 10
64

The smallest element is 1 and second Smallest element is 10
80_______45_______0

smallest element is: 1
second smallest element is: 10
35_______17_______68

The smallest element is 1 and second Smallest element is 10
80
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
69
smallest element is: 1
second smallest element is: 10
75
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
70

//C++ simple approach to print smallest9____26_______4

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

smallest element is 1
second smallest element is 9
79

Javascript




smallest element is 1
second smallest element is 9
80

 

smallest element : 1
second smallest element : 2
29

smallest element : 1
second smallest element : 2
30

 

smallest element : 1
second smallest element : 2
31
smallest element : 1
second smallest element : 2
32

smallest element : 1
second smallest element : 2
0

//C++ simple approach to print smallest9____45_______35

smallest element is: 1
second smallest element is: 10
35_______45_______37

 

//C++ simple approach to print smallest9____45_______39

//C++ simple approach to print smallest9_______17_______36

smallest element : 1
second smallest element : 2
42

//C++ simple approach to print smallest9____45_______0

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
90
smallest element : 1
second smallest element : 2
47
smallest element is 1
second smallest element is 9
94

smallest element : 1
second smallest element : 2
1
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
2
smallest element : 1
second smallest element : 2
51

//C++ simple approach to print smallest9____26_______4

smallest element : 1
second smallest element : 2
54____45_______55

//C++ simple approach to print smallest9_______17_______29

smallest element : 1
second smallest element : 2
58

//C++ simple approach to print smallest9____45_______0

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element : 1
second smallest element : 2
63

The smallest element is 1 and second Smallest element is 10
80_______45_______65

smallest element : 1
second smallest element : 2
1____26_______4

//C++ simple approach to print smallest9____26_______4

smallest element : 1
second smallest element : 2
54____45_______71

smallest element : 1
second smallest element : 2
54
smallest element is: 1
second smallest element is: 10
29
smallest element : 1
second smallest element : 2
74

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element : 1
second smallest element : 2
77

The smallest element is 1 and second Smallest element is 10
80_______45_______79

smallest element : 1
second smallest element : 2
1____26_______4

//C++ simple approach to print smallest9____26_______4

//C++ simple approach to print smallest9_______17_______36

smallest element : 1
second smallest element : 2
86

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
90
smallest element : 1
second smallest element : 2
89
smallest element is 1
second smallest element is 9
94

//C++ simple approach to print smallest9____45_______92

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
90
smallest element : 1
second smallest element : 2
95
smallest element : 1
second smallest element : 2
96
smallest element : 1
second smallest element : 2
97
smallest element is 1
second smallest element is 9
23

The smallest element is 1 and second Smallest element is 10
80//C++ simple approach to print smallest00 //C++ simple approach to print smallest01//C++ simple approach to print smallest02
smallest element is 1
second smallest element is 9
94

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

 

//C++ simple approach to print smallest9_______1191_______06

//C++ simple approach to print smallest9

//C++ simple approach to print smallest9____1191_______09

//C++ simple approach to print smallest9_______6_______85

//C++ simple approach to print smallest9_______1191_______13

//C++ simple approach to print smallest9

smallest element is: 1
second smallest element is: 10
01

Đầu ra

smallest element is: 1
second smallest element is: 10

 

Tìm số nhỏ nhất trong mảng javascript cho vòng lặp

Độ phức tạp về thời gian. TRÊN)
không gian phụ trợ. Ô(1)

Một giải pháp hiệu quả có thể tìm thấy hai yếu tố tối thiểu trong một lần duyệt. Dưới đây là thuật toán hoàn chỉnh
thuật toán.  

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second

Below is the implementation of the above approach

C++




smallest element is: 1
second smallest element is: 10
02

smallest element : 1
second smallest element : 2
30

smallest element is: 1
second smallest element is: 10
04

using

smallest element is 1
second smallest element is 9
0
smallest element is 1
second smallest element is 9
1//C++ simple approach to print smallest22

 

The smallest element is 1 and second Smallest element is 10
8 //C++ simple approach to print smallest24
smallest element is 1
second smallest element is 9
2 //C++ simple approach to print smallest26
smallest element is 1
second smallest element is 9
2 //C++ simple approach to print smallest28

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2 //C++ simple approach to print smallest32

 

smallest element : 1
second smallest element : 2
1
smallest element : 1
second smallest element : 2
39

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element : 1
second smallest element : 2
42

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______17_______6
smallest element : 1
second smallest element : 2
47
smallest element : 1
second smallest element : 2
51

smallest element is: 1
second smallest element is: 10
35_______26_______2
smallest element : 1
second smallest element : 2
51

smallest element : 1
second smallest element : 2
1____26_______4

 

smallest element : 1
second smallest element : 2
1//C++ simple approach to print smallest50

smallest element : 1
second smallest element : 2
1____17_______29 //C++ simple approach to print smallest53

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______1191_______57

smallest element is: 1
second smallest element is: 10
35_______1191_______59

smallest element is: 1
second smallest element is: 10
35_______17_______36 //C++ simple approach to print smallest62

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest66

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
65

smallest element is: 1
second smallest element is: 10
35_______26_______4

 

smallest element is: 1
second smallest element is: 10
35_______1191_______72

smallest element is: 1
second smallest element is: 10
35_______1191_______74

smallest element is: 1
second smallest element is: 10
35_______45_______92
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest78

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
79

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest85

smallest element is: 1
second smallest element is: 10
35_______17_______47____45_______89
smallest element : 1
second smallest element : 2
51

smallest element : 1
second smallest element : 2
1____45_______92

smallest element is: 1
second smallest element is: 10
35_______17_______47____45_______95 //C++ simple approach to print smallest95
smallest element : 1
second smallest element : 2
97

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest00 //C++ simple approach to print smallest99

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

//and second smallest element.01

smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
09

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1____6_______2
smallest element is: 1
second smallest element is: 10
13

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
16
smallest element is 1
second smallest element is 9
8
smallest element is 1
second smallest element is 9
9
smallest element is 1
second smallest element is 9
8
smallest element is: 1
second smallest element is: 10
1

smallest element : 1
second smallest element : 2
1//and second smallest element.16

smallest element : 1
second smallest element : 2
1____26_______2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
3

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

//and second smallest element.21

C




//and second smallest element.22

//and second smallest element.23

//and second smallest element.24

 

The smallest element is 1 and second Smallest element is 10
8 //C++ simple approach to print smallest24
smallest element is 1
second smallest element is 9
2 //C++ simple approach to print smallest26
smallest element is 1
second smallest element is 9
2 //C++ simple approach to print smallest28

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2 //C++ simple approach to print smallest32

 

smallest element : 1
second smallest element : 2
1
smallest element : 1
second smallest element : 2
39

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element : 1
second smallest element : 2
42

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______1192_______43
smallest element is 1
second smallest element is 9
21
smallest element : 1
second smallest element : 2
47
smallest element is 1
second smallest element is 9
94

smallest element is: 1
second smallest element is: 10
35_______26_______2
smallest element : 1
second smallest element : 2
51

smallest element : 1
second smallest element : 2
1____26_______4

 

smallest element : 1
second smallest element : 2
1//C++ simple approach to print smallest50

smallest element : 1
second smallest element : 2
1____17_______29 //C++ simple approach to print smallest53

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______1191_______57

//and second smallest element.61____1191_______59

smallest element is: 1
second smallest element is: 10
35_______17_______36 //C++ simple approach to print smallest62

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest66

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
65

smallest element is: 1
second smallest element is: 10
35_______26_______4

 

smallest element is: 1
second smallest element is: 10
35_______1191_______72

//and second smallest element.61____1192_______77

smallest element is: 1
second smallest element is: 10
35_______45_______92
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest78

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
79

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest85

smallest element is: 1
second smallest element is: 10
35_______1192_______43
smallest element is 1
second smallest element is 9
21
smallest element : 1
second smallest element : 2
89
smallest element is 1
second smallest element is 9
94

smallest element : 1
second smallest element : 2
1____45_______92

smallest element is: 1
second smallest element is: 10
35_______1192_______43
smallest element is 1
second smallest element is 9
21//and second smallest element.99

#include00#include01#include02

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

#include04

smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
09

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1____6_______2
smallest element is: 1
second smallest element is: 10
13

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
16
smallest element is 1
second smallest element is 9
8
smallest element is 1
second smallest element is 9
9
smallest element is 1
second smallest element is 9
8
smallest element is: 1
second smallest element is: 10
1

smallest element : 1
second smallest element : 2
1//and second smallest element.16

smallest element : 1
second smallest element : 2
1____26_______2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
3

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

Java




#include24

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
7
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
8

 

The smallest element is 1 and second Smallest element is 10
1 #include28

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1#include31

The smallest element is 1 and second Smallest element is 10
80#include33

smallest element : 1
second smallest element : 2
1
The smallest element is 1 and second Smallest element is 10
7
The smallest element is 1 and second Smallest element is 10
8 //C++ simple approach to print smallest24
smallest element is 1
second smallest element is 9
2 #include39

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______6_______2 #include44

 

smallest element is: 1
second smallest element is: 10
35_______45_______39

smallest element is: 1
second smallest element is: 10
35_______17_______36 #include49#include50#include51

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40#include0
smallest element : 1
second smallest element : 2
47
smallest element is 1
second smallest element is 9
94

smallest element is: 1
second smallest element is: 10
40
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
2
smallest element : 1
second smallest element : 2
51

smallest element is: 1
second smallest element is: 10
35_______26_______4

 

smallest element is: 1
second smallest element is: 10
35_______1193_______64

smallest element is: 1
second smallest element is: 10
35_______17_______29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
24#include3#include71

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest57

#include76//C++ simple approach to print smallest59

smallest element is: 1
second smallest element is: 10
40
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest62

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
0

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30____1191_______66

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30_______45_______65

smallest element is: 1
second smallest element is: 10
40
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest72

#include00//and second smallest element.77

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
92
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest78

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30_______45_______79

smallest element is: 1
second smallest element is: 10
35_______26_______4

smallest element is: 1
second smallest element is: 10
35_______17_______36 using03

smallest element is: 1
second smallest element is: 10
40#include0using06
smallest element is 1
second smallest element is 9
23

using08____1194_______09

smallest element is 1
second smallest element is 9
94

smallest element is: 1
second smallest element is: 10
35_______45_______92

smallest element is: 1
second smallest element is: 10
40#include0
smallest element : 1
second smallest element : 2
95
smallest element is 1
second smallest element is 9
23

using08using18using19

smallest element is 1
second smallest element is 9
23

using08using22 using23

smallest element : 1
second smallest element : 2
1____26_______4

 

smallest element : 1
second smallest element : 2
1using27

smallest element : 1
second smallest element : 2
1
The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
7
The smallest element is 1 and second Smallest element is 10
8 using32

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______6_______2
smallest element is: 1
second smallest element is: 10
97
smallest element is: 1
second smallest element is: 10
98
smallest element : 1
second smallest element : 2
5
smallest element : 1
second smallest element : 2
6
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4
smallest element : 1
second smallest element : 2
5
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
04
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest2
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4//C++ simple approach to print smallest5

smallest element is: 1
second smallest element is: 10
35_______1194_______51

smallest element : 1
second smallest element : 2
1____26_______4

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

using55

Python3




using56

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
7 using58

 

using59 using60

 

smallest element : 1
second smallest element : 2
1using62

smallest element : 1
second smallest element : 2
1using64_______1194_______8
smallest element is 1
second smallest element is 9
14
smallest element is 1
second smallest element is 9
15

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36 using70#include50using72

smallest element is: 1
second smallest element is: 10
35_______6_______20
smallest element is 1
second smallest element is 9
21using76#include51

smallest element is: 1
second smallest element is: 10
35_______26_______2

 

smallest element : 1
second smallest element : 2
1using81_______1194_______8 using83using8 using85

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
29
The smallest element is 1 and second Smallest element is 10
03_______36_______04
The smallest element is 1 and second Smallest element is 10
05
smallest element is 1
second smallest element is 9
21#include3using93

 

smallest element is: 1
second smallest element is: 10
35_______1194_______95

smallest element is: 1
second smallest element is: 10
35_______1194_______97

smallest element is: 1
second smallest element is: 10
35_______17_______36
smallest element is 1
second smallest element is 9
000

smallest element is: 1
second smallest element is: 10
40using83using8 using81

smallest element is: 1
second smallest element is: 10
40using81_______1194_______8
The smallest element is 1 and second Smallest element is 10
13

 

smallest element is: 1
second smallest element is: 10
35_______6_______010

smallest element is: 1
second smallest element is: 10
35_______6_______012

smallest element is: 1
second smallest element is: 10
35_______6_______014
smallest element is 1
second smallest element is 9
015
The smallest element is 1 and second Smallest element is 10
32
smallest element is 1
second smallest element is 9
017using8
smallest element is 1
second smallest element is 9
019

smallest element is: 1
second smallest element is: 10
40using83using8
smallest element is 1
second smallest element is 9
023

 

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element is 1
second smallest element is 9
026using8using8
smallest element is 1
second smallest element is 9
029

smallest element is: 1
second smallest element is: 10
35_______6_______20
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
033#include51

smallest element : 1
second smallest element : 2
1
smallest element : 1
second smallest element : 2
92using72

smallest element is: 1
second smallest element is: 10
35_______6_______20
smallest element is 1
second smallest element is 9
21_______6_______041
smallest element is 1
second smallest element is 9
042
smallest element is 1
second smallest element is 9
043_______6_______044

#include76____6_______046

smallest element is 1
second smallest element is 9
047

 

smallest element is 1
second smallest element is 9
048

using7using8 using9

smallest element is: 1
second smallest element is: 10
98
smallest element : 1
second smallest element : 2
5
smallest element : 1
second smallest element : 2
6
smallest element : 1
second smallest element : 2
5_______1191_______4
smallest element : 1
second smallest element : 2
5
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
04
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest2
smallest element : 1
second smallest element : 2
5//C++ simple approach to print smallest4_______6_______11

smallest element is 1
second smallest element is 9
064

 

smallest element is 1
second smallest element is 9
065

C#




smallest element is 1
second smallest element is 9
066

smallest element is 1
second smallest element is 9
067

using

smallest element is 1
second smallest element is 9
40

 

The smallest element is 1 and second Smallest element is 10
1
smallest element is 1
second smallest element is 9
43

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1

smallest element : 1
second smallest element : 2
1____6_______075

smallest element is 1
second smallest element is 9
076____6_______077

smallest element : 1
second smallest element : 2
1
The smallest element is 1 and second Smallest element is 10
7
The smallest element is 1 and second Smallest element is 10
8 //C++ simple approach to print smallest24
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
083

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______6_______2
smallest element is 1
second smallest element is 9
088

 

smallest element is: 1
second smallest element is: 10
35_______45_______39

smallest element is: 1
second smallest element is: 10
35_______17_______36
smallest element : 1
second smallest element : 2
42

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40
smallest element is 1
second smallest element is 9
097
smallest element : 1
second smallest element : 2
47
smallest element is 1
second smallest element is 9
94

smallest element is: 1
second smallest element is: 10
40
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
2
smallest element : 1
second smallest element : 2
51

smallest element is: 1
second smallest element is: 10
35_______26_______4

 

smallest element is: 1
second smallest element is: 10
35_______6_______106
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
108

smallest element is: 1
second smallest element is: 10
35

smallest element is: 1
second smallest element is: 10
35_______17_______29
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
114

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest57

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest59

smallest element is: 1
second smallest element is: 10
40
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest62

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
0

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30____1191_______66

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30_______45_______65

smallest element is: 1
second smallest element is: 10
40
1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest72

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest74

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
92
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest78

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
30_______45_______79

smallest element is: 1
second smallest element is: 10
35_______26_______4

smallest element is: 1
second smallest element is: 10
35_______17_______36
smallest element is 1
second smallest element is 9
146
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
148

smallest element is: 1
second smallest element is: 10
40
smallest element is 1
second smallest element is 9
097using06
smallest element is 1
second smallest element is 9
23

smallest element is 1
second smallest element is 9
153using09
smallest element is 1
second smallest element is 9
94

smallest element is: 1
second smallest element is: 10
35_______45_______92

smallest element is: 1
second smallest element is: 10
40
smallest element is 1
second smallest element is 9
097
smallest element : 1
second smallest element : 2
95
smallest element is 1
second smallest element is 9
23

smallest element is 1
second smallest element is 9
153_______1194_______18_______1194_______19
smallest element is 1
second smallest element is 9
23

smallest element is 1
second smallest element is 9
153using22 using23

smallest element : 1
second smallest element : 2
1____26_______4

 

smallest element : 1
second smallest element : 2
1using27

smallest element : 1
second smallest element : 2
1
The smallest element is 1 and second Smallest element is 10
6
The smallest element is 1 and second Smallest element is 10
7
The smallest element is 1 and second Smallest element is 10
8
smallest element is 1
second smallest element is 9
51

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______6_______2
smallest element is 1
second smallest element is 9
182

smallest element is: 1
second smallest element is: 10
35_______1194_______51

smallest element : 1
second smallest element : 2
1____26_______4

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

smallest element is 1
second smallest element is 9
188

PHP




smallest element is 1
second smallest element is 9
189

smallest element is 1
second smallest element is 9
190

smallest element : 1
second smallest element : 2
30

 

smallest element : 1
second smallest element : 2
31 //C++ simple approach to print smallest24
smallest element is 1
second smallest element is 9
194
smallest element : 1
second smallest element : 2
5
smallest element is 1
second smallest element is 9
196#include51

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1_______6_______200
smallest element is 1
second smallest element is 9
201

smallest element : 1
second smallest element : 2
1

smallest element : 1
second smallest element : 2
1____6_______204

smallest element is 1
second smallest element is 9
205
smallest element is 1
second smallest element is 9
206

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
196
smallest element is 1
second smallest element is 9
211

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______6_______215_______6_______21
smallest element : 1
second smallest element : 2
47
smallest element is 1
second smallest element is 9
94

smallest element is: 1
second smallest element is: 10
35_______26_______2
smallest element : 1
second smallest element : 2
51

smallest element : 1
second smallest element : 2
1____26_______4

 

smallest element : 1
second smallest element : 2
1
smallest element is 1
second smallest element is 9
225 using8
smallest element is 1
second smallest element is 9
227 using8
smallest element is 1
second smallest element is 9
200
smallest element : 1
second smallest element : 2
51

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
29
smallest element is 1
second smallest element is 9
21_______6_______234
smallest element is 1
second smallest element is 9
235
smallest element is 1
second smallest element is 9
234
smallest element is 1
second smallest element is 9
237
smallest element is 1
second smallest element is 9
196
smallest element : 1
second smallest element : 2
51
smallest element is 1
second smallest element is 9
234
smallest element is 1
second smallest element is 9
241

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35

smallest element is: 1
second smallest element is: 10
35_______6_______246

//and second smallest element.61____6_______248

//and second smallest element.61____6_______250

//and second smallest element.61____6_______252

smallest element is: 1
second smallest element is: 10
35_______17_______36
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
194using9
smallest element is 1
second smallest element is 9
234
smallest element is 1
second smallest element is 9
259
smallest element is 1
second smallest element is 9
225#include51

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40
smallest element is 1
second smallest element is 9
227 using8
smallest element is 1
second smallest element is 9
225
smallest element : 1
second smallest element : 2
51

smallest element is: 1
second smallest element is: 10
40
smallest element is 1
second smallest element is 9
225 using8
smallest element is 1
second smallest element is 9
194using9
smallest element is 1
second smallest element is 9
234
smallest element is 1
second smallest element is 9
275

smallest element is: 1
second smallest element is: 10
35_______26_______4

 

smallest element is: 1
second smallest element is: 10
35_______6_______279

//and second smallest element.61____6_______281

//and second smallest element.61____6_______283

smallest element is: 1
second smallest element is: 10
35_______45_______92
smallest element is: 1
second smallest element is: 10
36
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
194using9
smallest element is 1
second smallest element is 9
234
smallest element is 1
second smallest element is 9
259
smallest element is 1
second smallest element is 9
227
smallest element is 1
second smallest element is 9
293

smallest element is 1
second smallest element is 9
294
smallest element is 1
second smallest element is 9
194using9
smallest element is 1
second smallest element is 9
234
smallest element is 1
second smallest element is 9
298
smallest element is 1
second smallest element is 9
225#include51

smallest element is: 1
second smallest element is: 10
40
smallest element is 1
second smallest element is 9
227 using8
smallest element is 1
second smallest element is 9
194using9
smallest element is 1
second smallest element is 9
234
smallest element is 1
second smallest element is 9
275

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
227
smallest element is 1
second smallest element is 9
314
smallest element is 1
second smallest element is 9
200#include51

smallest element is: 1
second smallest element is: 10
35_______6_______215
smallest element is 1
second smallest element is 9
21
smallest element : 1
second smallest element : 2
89
smallest element is 1
second smallest element is 9
94

smallest element : 1
second smallest element : 2
1____45_______92

smallest element is: 1
second smallest element is: 10
35_______6_______215
smallest element : 1
second smallest element : 2
95
smallest element : 1
second smallest element : 2
5
smallest element is 1
second smallest element is 9
225

smallest element is 1
second smallest element is 9
329_______45_______5
smallest element is 1
second smallest element is 9
331

smallest element is 1
second smallest element is 9
332_______45_______5
smallest element is 1
second smallest element is 9
227
smallest element : 1
second smallest element : 2
51

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

The smallest element is 1 and second Smallest element is 10
5

smallest element is 1
second smallest element is 9
194 using8
smallest element is 1
second smallest element is 9
340
smallest element is 1
second smallest element is 9
341

smallest element is 1
second smallest element is 9
342 using8
smallest element is 1
second smallest element is 9
344
smallest element is 1
second smallest element is 9
21
smallest element is 1
second smallest element is 9
194
smallest element is 1
second smallest element is 9
94

//C++ simple approach to print smallest24

smallest element is 1
second smallest element is 9
194
smallest element : 1
second smallest element : 2
5
smallest element is 1
second smallest element is 9
342#include51

 

smallest element is 1
second smallest element is 9
353

smallest element is 1
second smallest element is 9
354

Javascript




smallest element is 1
second smallest element is 9
80

 

smallest element : 1
second smallest element : 2
29

smallest element : 1
second smallest element : 2
30

 

smallest element : 1
second smallest element : 2
31
smallest element is 1
second smallest element is 9
359

smallest element : 1
second smallest element : 2
0

smallest element : 1
second smallest element : 2
1____6_______362

 

smallest element : 1
second smallest element : 2
1
smallest element : 1
second smallest element : 2
39

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element : 1
second smallest element : 2
42

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______6_______90
smallest element : 1
second smallest element : 2
47
smallest element is 1
second smallest element is 9
94

smallest element is: 1
second smallest element is: 10
35_______26_______2
smallest element : 1
second smallest element : 2
51

smallest element : 1
second smallest element : 2
1____26_______4

 

smallest element : 1
second smallest element : 2
1____6_______380

smallest element : 1
second smallest element : 2
1____6_______382

smallest element : 1
second smallest element : 2
1____17_______29 //C++ simple approach to print smallest53

smallest element : 1
second smallest element : 2
1____45_______0

smallest element is: 1
second smallest element is: 10
35_______1191_______57

smallest element is: 1
second smallest element is: 10
35_______1191_______59

smallest element is: 1
second smallest element is: 10
35_______17_______36 //C++ simple approach to print smallest62

smallest element is: 1
second smallest element is: 10
35_______45_______0

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest66

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
65

smallest element is: 1
second smallest element is: 10
35_______26_______4

 

smallest element is: 1
second smallest element is: 10
35_______1191_______72

smallest element is: 1
second smallest element is: 10
35_______1191_______74

smallest element is: 1
second smallest element is: 10
35_______45_______92
smallest element is: 1
second smallest element is: 10
36 //C++ simple approach to print smallest78

smallest element is: 1
second smallest element is: 10
40
smallest element : 1
second smallest element : 2
79

smallest element : 1
second smallest element : 2
1____26_______4

smallest element : 1
second smallest element : 2
1
smallest element is: 1
second smallest element is: 10
36
smallest element : 1
second smallest element : 2
86

smallest element is: 1
second smallest element is: 10
35_______6_______90
smallest element : 1
second smallest element : 2
89
smallest element is 1
second smallest element is 9
94

smallest element : 1
second smallest element : 2
1____45_______92

smallest element is: 1
second smallest element is: 10
35_______6_______90_______45_______95
smallest element : 1
second smallest element : 2
96
smallest element : 1
second smallest element : 2
97_______6_______23

smallest element is: 1
second smallest element is: 10
40//C++ simple approach to print smallest00 //C++ simple approach to print smallest01//C++ simple approach to print smallest02
smallest element is 1
second smallest element is 9
94

1) Initialize both first and second smallest as INT_MAX
   first = second = INT_MAX
2) Loop through all the elements.
   a) If the current element is smaller than first, then update first 
       and second. 
   b) Else if the current element is smaller than second then update 
    second
4

 

 

smallest element : 1
second smallest element : 2
1//C++ simple approach to print smallest06

smallest element : 1
second smallest element : 2
1

smallest element : 1
second smallest element : 2
1//C++ simple approach to print smallest09

smallest element : 1
second smallest element : 2
1_______6_______85

smallest element : 1
second smallest element : 2
1//and second smallest element.16

smallest element : 1
second smallest element : 2
1

smallest element is: 1
second smallest element is: 10
01

Đầu ra

The smallest element is 1 and second Smallest element is 10

Cách tiếp cận tương tự có thể được sử dụng để tìm các phần tử lớn nhất và lớn thứ hai trong một mảng

Thời gian phức tạp. Trên)
Không gian phụ trợ. Ô(1)

Tiếp cận. Cách tiếp cận log(N) sử dụng cấu trúc dữ liệu Priority_queue. Bạn có thể đọc chi tiết hơn về Hàng đợi ưu tiên tại đây

C++




smallest element is: 1
second smallest element is: 10
04

using

smallest element is 1
second smallest element is 9
0
smallest element is 1
second smallest element is 9
1

 

smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
3

smallest element : 1
second smallest element : 2
1_______6_______2
smallest element is 1
second smallest element is 9
455

The smallest element is 1 and second Smallest element is 10
80
smallest element is 1
second smallest element is 9
2
smallest element is: 1
second smallest element is: 10
16
smallest element is 1
second smallest element is 9
8
smallest element is: 1
second smallest element is: 10
18
smallest element is 1
second smallest element is 9
8
smallest element is: 1
second smallest element is: 10
1

The smallest element is 1 and second Smallest element is 10
80
smallest element is 1
second smallest element is 9
464
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
466
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
468
smallest element is 1
second smallest element is 9
2
smallest element is 1
second smallest element is 9
470
smallest element is 1
second smallest element is 9
471

Làm cách nào để tìm số nhỏ nhất trong mảng JavaScript bằng vòng lặp?

Sử dụng hai vòng lặp . Initialize the array arr . Khởi tạo tần số bằng 0. Khởi tạo tối thiểu với phần tử đầu tiên trong mảng. Sử dụng vòng lặp đầu tiên để tìm ra phần tử tối thiểu trong mảng.

Làm cách nào để tìm số tối thiểu từ mảng trong JavaScript?

Toán. hàm min() thường lấy tất cả các phần tử trong một mảng và xem xét kỹ lưỡng từng giá trị để lấy giá trị nhỏ nhất.

Làm cách nào để tìm hai số nhỏ nhất trong mảng js?

Sắp xếp mảng tăng dần. Sử dụng Array#slice để lấy hai phần tử đầu tiên (phần tử nhỏ nhất).