diff --git a/snippets/gcdOfArray.md b/snippets/gcdOfArray.md new file mode 100644 index 000000000..4865ebb14 --- /dev/null +++ b/snippets/gcdOfArray.md @@ -0,0 +1,12 @@ +### gcdOfArray + +It finds the GCD of all the numbers in an array by using `array.reduce` and the fact that `gcd(a,b,c) = gcd(gcd(a,b),c)` + +```js +const gcdOfArray = arr => + { + const gcd = (x, y) => !y ? x : gcd(y, x % y); + arr.reduce((a,b) => gcd(a,b)) + } +// functionName(sampleInput) -> sampleOutput +```