← Back to challenges

Common Divisor of Array

JavaScriptHardalgebraalgorithmsmathnumbers

Instructions

Write a function that returns the greatest common divisor of all array elements. If the greatest common divisor is 1, return 1.

Examples

gcd([10, 20, 40]) ➞ 10

gcd([1, 2, 3, 100]) ➞ 1

gcd([1024, 192, 2048, 512]) ➞ 64

Notes

  • Array elements are always greater than 0.
  • There is a minimum of two array elements given.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Rearrange the Number