> For the complete documentation index, see [llms.txt](https://songeun.gitbook.io/coding-test/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://songeun.gitbook.io/coding-test/lv.0/53.md).

# 직사각형 넓이 구하기

## **문제 설명**

2차원 좌표 평면에 변이 축과 평행한 직사각형이 있습니다. 직사각형 네 꼭짓점의 좌표 \[\[x1, y1], \[x2, y2], \[x3, y3], \[x4, y4]]가 담겨있는 배열 `dots`가 매개변수로 주어질 때, 직사각형의 넓이를 return 하도록 solution 함수를 완성해보세요.

***

**제한사항**

* `dots`의 길이 = 4
* `dots`의 원소의 길이 = 2
* -256 < `dots[i]`의 원소 < 256
* 잘못된 입력은 주어지지 않습니다.

***

**입출력 예**

| dots                                      | result |
| ----------------------------------------- | ------ |
| \[\[1, 1], \[2, 1], \[2, 2], \[1, 2]]     | 1      |
| \[\[-1, -1], \[1, 1], \[1, -1], \[-1, 1]] | 4      |

***

**입출력 예 설명**

입출력 예 #1

* 좌표 \[\[1, 1], \[2, 1], \[2, 2], \[1, 2]] 를 꼭짓점으로 갖는 직사각형의 가로, 세로 길이는 각각 1, 1이므로 직사각형의 넓이는 1 x 1 = 1입니다.

입출력 예 #2

* 좌표 \[\[-1, -1], \[1, 1], \[1, -1], \[-1, 1]]를 꼭짓점으로 갖는 직사각형의 가로, 세로 길이는 각각 2, 2이므로 직사각형의 넓이는 2 x 2 = 4입니다.

## 코드

```javascript
function solution(dots) {
    let height = Math.abs(dots.filter(dot => dots[0][0] === dot[0]).pop()[1] - dots[0][1]);
    let width = Math.abs(dots.filter(dot => dots[0][1] === dot[1]).pop()[0] - dots[0][0]);
    return width*height
}
```

### 다른 풀이

```javascript
function solution(dots) {
    let x = [...new Set(dots.map(p => p[0]))]
    let y = [...new Set(dots.map(p => p[1]))]
    return Math.abs((x[0] - x[1]) * (y[0] - y[1]))
}
```

(1) 배열에서 ...new Set 함수를 통해 중복값을 제외하고

(2) x 좌표의 차, y 좌표의 차를 각각 절대값으로 계산해주었다. 깔끔..!
