Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] OddOccurrencesInArray #130

Closed
hwangJi-dev opened this issue Mar 21, 2023 · 0 comments
Closed

[Algorithm] OddOccurrencesInArray #130

hwangJi-dev opened this issue Mar 21, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Mar 21, 2023

💬 문제

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant