# 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
``1export function findMin(nums) {2  let min = Infinity;34  for (let i = 0; i < nums.length; i++) {5    if (nums[i] < min) min = nums[i];6  }78  return min;9}``
``3export function findMin(nums) {4  if (nums[0] <= nums[nums.length - 1]) return nums[0];56  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  }1213  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;45  while (i >= 0) {6    if (nums[i] > nums[i + 1]) return nums[i + 1];7    i--;8  }910  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 a7	if (b > a) return b8	return null9}``
``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;56  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    }1314    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  }2122  return ans + 1;23}``
``1export function findShortestSubArray(nums: number[]) {2  const res = []3  let maxTimes = 04  let ans = 056  for (let i = 0; i < nums.length; i++) {7    if (res[nums[i]]) {8      res[nums[i]].right = i9      res[nums[i]].times++10    } else {11      res[nums[i]] = { left: i, right: i, times: 1 }12    }1314    if (maxTimes < res[nums[i]].times) {15      maxTimes = res[nums[i]].times16      ans = res[nums[i]].right - res[nums[i]].left17    } else if (maxTimes === res[nums[i]].times) {18      ans = Math.min(ans, res[nums[i]].right - res[nums[i]].left)19    }20  }2122  return ans + 123}``
``11export function findFirst(array, p) {12    var low = 0, high = array.length;13    if (high === 0) {14        return 0; // no children15    }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 children16  }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}``