Skip to content

[Algorithm] OddOccurrencesInArray #130

Closed
@hwangJi-dev

Description

@hwangJi-dev

💬 문제

https://app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/


💬 Idea

  • 배열의 count를 dictionary에 저장한 다음, 짝수개를 가지는 key를 모두 제거하고 남는 하나의 key를 리턴한다.

💬 풀이

import Foundation

public func solution(B : inout [Int]) -> Int {
    var countDict: [Int: Int] = [:]

    for i in B {
        if countDict[i] == nil {
            countDict[i] = 1
        } else {
            countDict[i]! += 1
        }
    }

    return countDict.filter{ $0.value % 2 == 1 }.first!.key
}

소요시간 : 11분

https://app.codility.com/demo/results/training95K8QQ-KYV/

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions