10 examples of 'find smallest number in array' in JavaScript

Every line of 'find smallest number in array' 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
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}
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

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}
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}
5function largestNumber(a, b) {
6 if (a > b) return a
7 if (b > a) return b
8 return null
9}
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}
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}
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}
11export function findFirst(array, p) {
12 var low = 0, high = array.length;
13 if (high === 0) {
14 return 0; // no children
15 }
16 while (low < high) {
17 var mid = Math.floor((low + high) / 2);
18 if (p(array[mid])) {
19 high = mid;
20 }
21 else {
22 low = mid + 1;
23 }
24 }
25 return low;
26}
12export function findFirst<T>(array: T[], p: (x: T) => boolean): number {
13 let low = 0, high = array.length;
14 if (high === 0) {
15 return 0; // no children
16 }
17 while (low < high) {
18 let mid = Math.floor((low + high) / 2);
19 if (p(array[mid])) {
20 high = mid;
21 } else {
22 low = mid + 1;
23 }
24 }
25 return low;
26}

Related snippets