
ํ๋ฉด์ n๊ฐ์ ์ ์ด ์๋ค. ๊ทธ์ค ๋ ๊ฐ ์ด์์ ์ ์ ์ง๋๋ฉด์ x์ถ ๋๋ y์ถ์ ํํํ ์ง์ ์ด ๋ช ๊ฐ์ธ์ง ์์๋ด๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
4
0 0
10 10
0 10
10 0
์ฒซ์งธ ์ค์ ๋ต์ ์ถ๋ ฅ
4
์ฃผ์ด์ง ์ขํ์ ์ ์ด ์๊ณ , 2๊ฐ ์ด์์ ์ ์ ๋์์ ์ง๋๋ ์ง์ ๋ง ์ธ๋ ๋ฌธ์ ์ ๋๋ค. ์ฆ, ๊ฐ x์ขํ๋ณ๋ก 2๊ฐ ์ด์ ๋ฑ์ฅํ๋ ์ธ๋ก์ ๊ณผ, ๊ฐ y์ขํ๋ณ๋ก 2๊ฐ ์ด์ ๋ฑ์ฅํ๋ ๊ฐ๋ก์ ์ ์ธ๋ ๊ฒ์ ๋๋ค.
๋ฐ๋ผ์ ์ฃผ์ด์ง ์ขํ์ ์ ์ x์ขํ๋ณ๋ก, y์ขํ๋ณ๋ก ์นด์ดํธํด์ผํฉ๋๋ค.
const countX = new Map();
const countY = new Map();
const coordinates = input.slice(1).map(line => line.split(' ').map(Number));
for (const [x, y] of coordinates) {
countX.set(x, (countX.get(x) || 0) + 1);
countY.set(y, (countY.get(y) || 0) + 1);
}
์ด์ ๊ฐ๊ฐ ๊ฐ์ด 2 ์ด์์ธ key๋ฅผ ์ธ์ด์ฃผ๋ฉด ๋ฉ๋๋ค.
let result = 0;
for (const [x, cnt] of countX) {
if (cnt >= 2) result += 1;
}
for (const [y, cnt] of countY) {
if (cnt >= 2) result += 1;
}
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const n = Number(input[0]);
const coordinates = input.slice(1).map(line => line.split(' ').map(Number));
const countX = new Map();
const countY = new Map();
for (const [x, y] of coordinates) {
countX.set(x, (countX.get(x) || 0) + 1);
countY.set(y, (countY.get(y) || 0) + 1);
}
let result = 0;
for (const [x, cnt] of countX) {
if (cnt >= 2) result += 1;
}
for (const [y, cnt] of countY) {
if (cnt >= 2) result += 1;
}
console.log(result);

countX ์ countY ๋ณ๋ก ์ธ๋ ๋ถ๋ถ์ for๋ฌธ์ด ์ค๋ณต๋์ด์ ๋ฆฌํฉํ ๋ง์ ํด๋ณด๊ฒ ์ต๋๋ค.
const countLines = (map) => {
return [...map.values()].filter(count => count >= 2).length;
}
console.log(countLines(countX) + countLines(countY));
ํจ์๋ฅผ ๋ง๋ค์ด ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ฉด์ ํ์ ์๋ ๋ณ์๋ฅผ ์ค์ฌ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ์ฝํ ์ ์์ต๋๋ค.
