10 examples of 'find second largest number in array javascript' in JavaScript

Every line of 'find second largest number in array javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5function largestNumber(a, b) {
6 if (a > b) return a
7 if (b > a) return b
8 return null
9}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

9function largest(arr, n){
10 max = arr[0];
11 min = arr[0];
12 count = 1;
13 while (count <= n) {
14 var min2 = min;
15 for (var x = 0; x < arr.length; x++) {
16 if (count == 1) {
17 if (arr[x] > max){
18 max = arr[x];
19 min2 = max;
20 }
21 if (arr[x] < min) {
22 min = arr[x];
23 }
24 } else {
25 if (arr[x] < max && arr[x] > min2) {
26 min2 = arr[x];
27 }
28 }
29 }
30 max = min2;
31 count++;
32 }
33 return max;
34}
17function getMaxNumber(arr) {
18 var newArr = quickSort(arr);
19 return newArr[newArr.length - 1];
20}
72function findMedianOfArrayAndValue(array, value) {
73 var arrayMedian = medianOfArray(array);
74 if (arrayMedian === value) {
75 return arrayMedian;
76 }
77 if (array.length % 2 === 0) {
78 return findMedianOfArrayAndValueEvenCase(array, arrayMedian, value);
79 }
80 return findMedianOfArrayAndValueOddCase(array, arrayMedian, value);
81}
1export function findShortestSubArray(nums: number[]) {
2 const res = []
3 let maxTimes = 0
4 let ans = 0
5
6 for (let i = 0; i < nums.length; i++) {
7 if (res[nums[i]]) {
8 res[nums[i]].right = i
9 res[nums[i]].times++
10 } else {
11 res[nums[i]] = { left: i, right: i, times: 1 }
12 }
13
14 if (maxTimes < res[nums[i]].times) {
15 maxTimes = res[nums[i]].times
16 ans = res[nums[i]].right - res[nums[i]].left
17 } else if (maxTimes === res[nums[i]].times) {
18 ans = Math.min(ans, res[nums[i]].right - res[nums[i]].left)
19 }
20 }
21
22 return ans + 1
23}
1export function findShortestSubArray(nums) {
2 const res = [];
3 let maxTimes = 0;
4 let ans = 0;
5
6 for (let i = 0; i < nums.length; i++) {
7 if (res[nums[i]]) {
8 res[nums[i]].right = i;
9 res[nums[i]].times++;
10 } else {
11 res[nums[i]] = { left: i, right: i, times: 1 };
12 }
13
14 if (maxTimes < res[nums[i]].times) {
15 maxTimes = res[nums[i]].times;
16 ans = res[nums[i]].right - res[nums[i]].left;
17 } else if (maxTimes === res[nums[i]].times) {
18 ans = Math.min(ans, res[nums[i]].right - res[nums[i]].left);
19 }
20 }
21
22 return ans + 1;
23}
171function findMax(arr) {
172 let record = 0;
173 let index = 0;
174 for (let i = 0; i < arr.length; i++) {
175 if (arr[i] > record) {
176 record = arr[i];
177 index = i;
178 }
179 }
180 return index;
181
182}
3export function findMin(nums) {
4 if (nums[0] <= nums[nums.length - 1]) return nums[0];
5
6 let left = 0;
7 let right = nums.length - 1;
8 while (left < right) {
9 const pivot = ~~((left + right) / 2);
10 nums[pivot] > nums[right] ? (left = pivot + 1) : (right = pivot);
11 }
12
13 return nums[left];
14}
1export function findMin(nums) {
2 if (nums[0] <= nums[nums.length - 1]) return nums[0];
3 let i = nums.length - 2;
4
5 while (i >= 0) {
6 if (nums[i] > nums[i + 1]) return nums[i + 1];
7 i--;
8 }
9
10 return nums[nums.length - 1];
11}
1export function findMin(nums) {
2 let min = Infinity;
3
4 for (let i = 0; i < nums.length; i++) {
5 if (nums[i] < min) min = nums[i];
6 }
7
8 return min;
9}

Related snippets