만약 배열 내 중복값이 있는지 없는지 찾기위해서는?
func hasDuplicate(_ary []int) bool {
step := 0
result := false
for i := 0; i < len(_ary); i++ {
for j := 0; j < len(_ary); j++ {
step++
if i != j {
if _ary[i] == _ary[j] {
return true
}
}
}
}
fmt.Println("step :", step)
return result
}
이를 선형해결법으로 개선해보자면?
func linearSolution(_ary []int) bool {
tempMap := make(map[int]bool)
result := false
for i := 0; i < len(_ary); i++ {
if tempMap[_ary[i]] == true {
result = true
} else {
tempMap[_ary[i]] = true
}
}
return result
}
'Web Development > CommonSense' 카테고리의 다른 글
[AL&DS] 큐 (0) | 2022.11.12 |
---|---|
[AL&DS] 스택 (0) | 2022.11.12 |
[AL&DS] 삽입정렬 (0) | 2022.11.12 |
[AL&DS] 버블정렬 (0) | 2022.11.06 |
[AL&DS] 선형검색, 이진검색 (0) | 2022.11.06 |