forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleastCommonMultipleArray.js
32 lines (30 loc) · 1.04 KB
/
leastCommonMultipleArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm';
/**
* Function to find the least common multiple of an array of numbers.
* @param {Array<number>} nums Array of numbers.
* @return {number}
* @throws {Error("Array is empty")} - Thrown when the input array is empty.
*/
export default function leastCommonMultipleArray(nums) {
// Remove duplicates from array
const uniqueNums = [...new Set(nums)];
// Checks if array is empty then throw error
if (uniqueNums.length === 0) {
throw new Error('Array is empty');
}
// Checks if array contains 0 then return 0 as LCM
for (let i = 0; i < uniqueNums.length; i += 1) {
if (uniqueNums[i] === 0) {
return 0;
}
}
// Initialize LCM with first element of array
let lcm = Math.abs(uniqueNums[0]);
// Iterate over the array and find LCM of each element
for (let i = 1; i < uniqueNums.length; i += 1) {
const currentGCD = euclideanAlgorithm(lcm, uniqueNums[i]);
lcm = Math.abs(lcm * uniqueNums[i]) / currentGCD;
}
// Return LCM
return lcm;
}