Arrange 2 objects in a tuple by their length. Useful for situations where you are iterating a or b depending on which is larger.
a
b
Time complexity: O(1)
Space complexity: O(1)
First object
Second object
const a = [1, 2];const b = [1, 2, 3];const [largest, smallest] = largeToSmall(a, b);console.log(largest); // [1, 2, 3]console.log(smallest); // [1, 2] Copy
const a = [1, 2];const b = [1, 2, 3];const [largest, smallest] = largeToSmall(a, b);console.log(largest); // [1, 2, 3]console.log(smallest); // [1, 2]
If a does not have a length or size property
length
size
Array
Arrange 2 objects in a tuple by their size. Useful for situations where you are iterating a or b depending on which is larger.
const a = new Set([1, 2]);const b = new Set([1, 2, 3]);const [largest, smallest] = largeToSmall(a, b);console.log(largest); // Set(3) { 1, 2, 3 }console.log(smallest); // Set(2) { 1, 2 } Copy
const a = new Set([1, 2]);const b = new Set([1, 2, 3]);const [largest, smallest] = largeToSmall(a, b);console.log(largest); // Set(3) { 1, 2, 3 }console.log(smallest); // Set(2) { 1, 2 }
Arrange 2 objects in a tuple by their length. Useful for situations where you are iterating
a
orb
depending on which is larger.Time complexity: O(1)
Space complexity: O(1)