# LeetCode: Find Greatest Common Divisor of Array Solution

Straight forward: min, max then gcd

## Approach

Straight forward:

• find max
• find min
• find gcd between max of min

JavaScript has no built-in gcd function, so we have to implement from scratch

`.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}function gcd(a, b) {2  if (a % b === 0) {3    return b4  }5  return gcd(b, a % b)6}`

## Implementation

```1var findGCD = function (nums) {2  function gcd(a, b) {3    if (a % b === 0) {4      return b5    }6    return gcd(b, a % b)7  }8
9  const max = Math.max.apply(null, nums)10  const min = Math.min.apply(null, nums)11
12  return gcd(max, min)13}```

leetcode

array

math

## Next Post

LeetCode: Length of Last Word

Aug 23, 2021

One-line KO

## Previous Post

LeetCode: Rotate List

Aug 22, 2021

Should draw for better imagination of how it works

Search Posts