Skip to content

Commit 4b62523

Browse files
authored
Create SockMerchant.swift
1 parent c055f65 commit 4b62523

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://www.hackerrank.com/challenges/sock-merchant/problem
2+
import Foundation
3+
4+
// Complete the sockMerchant function below.
5+
// 양말 짝 맞추기
6+
// 10 20 20 10 10 30 50 10 20 일 때 짝이 되는 갯수는 3 (10, 10) (10, 10) (20, 20)
7+
func sockMerchant(n: Int, ar: [Int]) -> Int {
8+
if n >= 1 && n <= 100 { "invalid" }
9+
var counts = [Int]()
10+
var result: Int = 0
11+
12+
let uniqueArray = Array(Set(ar))
13+
14+
for i in 0..<uniqueArray.count {
15+
var num = 0
16+
for j in 0..<ar.count {
17+
if uniqueArray[i] == ar[j] {
18+
num += 1
19+
}
20+
}
21+
counts.append(num)
22+
}
23+
24+
for i in 0..<counts.count {
25+
result += counts[i] / 2
26+
}
27+
28+
return result
29+
}

0 commit comments

Comments
 (0)