์ต๋น๊ฐ์ ์ฃผ์ด์ง ๊ฐ ์ค์์ ๊ฐ์ฅ ์์ฃผ ๋์ค๋ ๊ฐ์ ์๋ฏธํฉ๋๋ค.
์ ์ ๋ฐฐ์ด array๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ต๋น๊ฐ์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด๋ณด์ธ์.
์ต๋น๊ฐ์ด ์ฌ๋ฌ ๊ฐ๋ฉด -1์ return ํฉ๋๋ค.
array result
[1, 2, 3, 3, 3, 4] 3
[1, 1, 2, 2] -1
[1] 1
์
์ถ๋ ฅ ์ #1
[1, 2, 3, 3, 3, 4]์์ 1์ 1๊ฐ 2๋ 1๊ฐ 3์ 3๊ฐ 4๋ 1๊ฐ๋ก ์ต๋น๊ฐ์ 3์
๋๋ค.

times_dict = {} # ๋น์ด ์๋ ๋์
๋๋ฆฌ๋ฅผ ์์ฑ
for num in array: # ๋งค๊ฐ๋ณ์ array ์์ ์์ฐจ
{ array์์ : ์ต๋น๊ฐ } ๋์
๋๋ฆฌ๋ฅผ ์์ฑํ๊ณ ์นด์ดํฐ๋ฅผ ์ฆ๊ฐ์ํค์
if num not in times_dict: # ๋น ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์
times_dict[num] = 0 # key์ value ๋ฅผ ์์ฑ
times_dict[num] += 1 # ์นด์ดํฐ ์ฆ๊ฐ
mode_times = second_times = 0 # mode_times : ์ต๋น๊ฐ์ ํ์ / second_times : ์ต๋น๊ฐ๊ณผ ๊ฐ์ ํ์๊ฐ ๋์ฌ ๊ฒฝ์ฐ
for num, times in times_dict.items(): # ๋์
๋๋ฆฌ {num:times} ์ฐจ๋ก๋ก ๋ฐ๋ณต
if times > mode_times: # ๋ฐ๋ณต๋ฌธ์ด๋๊น ์์ฐจ์ ์ผ๋ก ๋น๊ต , ์ด๊ธฐ์ mode_times๋ 0 , ๊ทธ๋ค์์ ์ต๋น๊ฐ์ ํ์
mode = num # ์ต๋น๊ฐ ์ค์
mode_times = times # ์ต๋น๊ฐ์ ํ์ ์ค์
elif times == mode_times: # ์ต๋น๊ฐ์ ํ์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ
second_times = times # second_times ์ ์ ์ฅ
return mode if mode_times != second_times else -1 # ์ต๋น๊ฐ์ด ์ฌ๋ฌ ๊ฐ๋ฉด -1์ด return
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๋์
๋๋ฆฌ
๋์ ๊ด๊ณ๋ฅผ ๋ํ๋ผ ์ ์๋ ์๋ฃํ
Key์ Value๋ฅผ ํ ์์ผ๋ก ๊ฐ๋ ์๋ฃํ
{Key1:Value1, Key2:Value2, Key3:Value3, ...}
>>> a.items()
items ํจ์๋ Key์ Value์ ์์ ํํ๋ก ๋ฌถ์ ๊ฐ์ dict_items ๊ฐ์ฒด๋ก ๋๋ ค์ค๋ค.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
์งํฉ์ ์ด์ฉํ์ฌ๋ ํ์ ์๋ค .
set(์งํฉ)
์์๊ฐ ์๊ณ , ์งํฉ์์์๋ uniqueํ ๊ฐ์ ๊ฐ์ง๋๋ค.
์ค๊ดํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ dictionary์ ๋น์ทํ์ง๋ง, key๊ฐ ์์ต๋๋ค. ๊ฐ๋ง ์กด์ฌํฉ๋๋ค.
์ค๋ณต๋ ๊ฐ์ ์๋์ผ๋ก ์ค๋ณต์ด ์ ๊ฑฐ ๋ฉ๋๋ค.
>>> s = {1, 5, 1, 1, 1, 3, 7}
>>> s
{1, 3, 5, 7}
