Altiora Petamus

Odd Occurrences In Array ๋ณธ๋ฌธ

1day-1algorithm

Odd Occurrences In Array

Haril Song 2021. 6. 18. 14:28
 

OddOccurrencesInArray coding task - Learn to Code - Codility

Find value that occurs in odd number of elements.

app.codility.com

๐Ÿค”์ƒ๊ฐํ•ด๋ณด๊ธฐ

Arrays ๊ด€๋ จ ๋ฌธ์ œ๋‹ค๋ณด๋‹ˆ ์—ฌ๋Ÿฌ ํ’€์ด ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

๊ทธ ์ค‘์— ํ•„์ž๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ๋– ์˜ค๋ฅธ ๋ฐฉ๋ฒ•์€ ๋ฌธ์ œ์˜ ๋งˆ์ง€๋ง‰์— ์ ํ˜€์žˆ๋Š” ์กฐ๊ฑด๊ณผ ๊ด€๋ จ์ด ์žˆ๋‹ค.

all but one of the values in A occur an even number of times.
"A ์˜ ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’์€ ์ง์ˆ˜๋กœ๋งŒ ๋ฐœ์ƒํ•œ๋‹ค"

๋‹จ ํ•˜๋‚˜๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’์ด ์ง์ˆ˜๋กœ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ํ™€์ˆ˜๋กœ ๋ฐœ์ƒํ•˜๋Š” ๊ทธ ํ•˜๋‚˜๋ฅผ ์ง‘์–ด๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉด ๋˜๋Š” ๊ฒƒ ์•„๋‹Œ๊ฐ€?

  • ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ์ง€๋งŒ ํŒŒ์ด์ฌ์€ Counter ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ์•„์ฃผ ์‰ฝ๊ฒŒ ๋”•์…”๋„ˆ๋ฆฌ ๊ตฌ์กฐ๋กœ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
  • ํ™€์ˆ˜๋กœ ๋ฐœ์ƒํ•œ element ๊ณจ๋ผ๋‚ด๊ธฐ → filter ๋ฅผ ์‚ฌ์šฉํ•˜์ž.

Total score 100%

from collections import Counter


def solution(A):
    n = Counter(A)
    return list(filter(lambda x: x[1] % 2 != 0, n.items()))[0][0]

๊ฐœ์ธ์ ์œผ๋กœ Codility ์˜ ๋ฌธ์ œ๋“ค์€ ์ง€๋‚˜์น˜๊ฒŒ ๋‹จ์ˆœํ•˜์ง€ ์•Š๊ณ  ํ€ผ๋ฆฌํ‹ฐ๊ฐ€ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.

'1day-1algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ํ† ๋งˆํ†   (0) 2021.06.19
์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ (์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด)  (0) 2021.06.18
ํ„ฐ๋ ›  (0) 2021.06.17
๋„ค ๋ฒˆ์งธ ์   (0) 2021.06.16
์ˆ˜ ์ฐพ๊ธฐ  (0) 2021.06.15