TheAlgorithms/JavaScript · error · Error
Provided input is not an array
Error message
Provided input is not an array
What it means
generateSubSets builds the power set by indexing inputArray[j] for each set bit, so it requires a real Array. The guard uses Array.isArray to reject iterables that are not arrays (Sets, strings, generators) which would break the indexed access.
Source
Thrown at Bit-Manipulation/GenerateSubSets.js:11
/**
* @function generateSubSets
* @param {Array} inputArray
* @returns {Array}
* @example [1,2] -> [[],[1],[2],[1,2]]
*/
// The time complexity of this algorithm is BigO(2^n) where n is the length of array
function generateSubSets(inputArray) {
if (!Array.isArray(inputArray)) {
throw new Error('Provided input is not an array')
}
if (inputArray.length > 32) {
throw new RangeError('Error size should be less than equal to 32')
}
let arrayLength = inputArray.length
let subSets = []
// loop till (2^n) - 1
for (let i = 0; i < 1 << arrayLength; i++) {
let subSet = []
for (let j = 0; j < arrayLength; j++) {
// 1 << j it shifts binary digit 1 by j positions and then we perform
// and by AND operation we are checking whetheer jth bit
// in i is set to 1 if result is non zero just add into set
if (i & (1 << j)) {
subSet.push(inputArray[j])
}
}
subSets.push(subSet)View on GitHub (pinned to 5c39e87a9a)
Solutions
- Pass an Array: generateSubSets([1,2,3]).
- Spread other iterables first: generateSubSets([...mySet]).
- For characters, split the string: generateSubSets(str.split('')).
Example fix
// before generateSubSets(new Set([1,2,3])) // after generateSubSets([...new Set([1,2,3])])
Defensive patterns
Strategy: validation
Validate before calling
function subsetsSafe(iter) {
const arr = Array.isArray(iter) ? iter : Array.from(iter);
return generateSubSets(arr);
} Type guard
/** @param {unknown} x @returns {x is unknown[]} */
const isArray = x => Array.isArray(x); Try / catch
try { return generateSubSets(input); }
catch (e) {
if (e instanceof Error && /not an array/.test(e.message) && input != null) {
return generateSubSets(Array.from(input));
}
throw e;
} Prevention
- Spread Sets/iterables before calling: generateSubSets([...set]).
- Split strings into char arrays when character subsets are wanted.
- Wrap external collections with Array.from at the boundary.
When it happens
Trigger: Passing a Set, a string, a Map, an object, null, undefined, a NodeList, or any non-Array iterable.
Common situations: Converting a Set to subsets without [...set], passing a string expecting character subsets, or handing in a library collection object.
Related errors
- Argument not an Integer
- Error size should be less than equal to 32
- Invalid capacity
- Rule must be an integer between the values 0 and 255 (got ${
- Coefficient a, b should be number
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/2c39938b82329f73.
Report an issue: GitHub.