← Back to challenges

Finding the Number of Rectangles

JavaScriptHardgeometrymath

Instructions

You are given an array of distinct (x, y) coordinates. Create a function that returns how many rectangles these points form on the plane.

Examples

rectangles([[1, 1], [2, 1], [1, 2], [2, 2]]) ➞ 1

rectangles([[1, 1], [2, 1], [1, 2], [2, 2], [3, 1], [3, 2]]) ➞ 3

rectangles([[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2], [3, 3], [2, 3], [1, 3]]) ➞ 10
// Note: We have a rectangle with vertices (2, 1), (3, 2), (2, 3), and (1, 2).

Notes

Don't forget to count rectangles that aren't parallel to the x- and y-axis (see example #3).

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.