๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โŒจ๏ธ Language/swift

[Swift] Codility Lesson 2 - OddOccurrencesInArray

by hyebin (Helia) 2022. 7. 23.
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

For example, in array A such that:

A[0] = 9, A[1] = 3, A[2] = 9, A[3] = 3, A[4] = 9, A[5] = 7, A[6] = 9

the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
the elements at indexes 4 and 6 have value 9,
the element at index 5 has value 7 and is unpaired.

Write a function:

public func solution(_ A : inout [Int]) -> Int

that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.

For example, given array A such that:

A[0] = 9, A[1] = 3, A[2] = 9, A[3] = 3, A[4] = 9, A[5] = 7, A[6] = 9

the function should return 7, as explained in the example above.

Write an efficient algorithm for the following assumptions:

N is an odd integer within the range [1..1,000,000];

each element of array A is an integer within the range [1..1,000,000,000];

all but one of the values in A occur an even number of times.

 

๋งํฌ

 

OddOccurrencesInArray coding task - Learn to Code - Codility

Find value that occurs in odd number of elements.

app.codility.com

 

๋ฌธ์ œ ๋ถ„์„

  • N๊ฐœ์˜ ์ •์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด A๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. (N์€ ํ™€์ˆ˜)
  • ๋ฐฐ์—ด A์—์„œ ํ•œ ์š”์†Œ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค๋ผ๋ฆฌ๋Š” ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ–๋Š” ์š”์†Œ๋ผ๋ฆฌ ์Œ์„ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.
  • ์Œ์„ ์ด๋ฃจ์ง€ ์•Š๋Š” ์š”์†Œ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
ex) A = [4, 6, 8, 9, 9, 6, 4]

A[0] == A[6] => 4
A[1] == A[5] => 6
A[3] == A[4] => 9

=> A[2] = 8

 

ํ•ด๊ฒฐ๋ฐฉ์•ˆ 1 - ์‹คํŒจ

  1. ๋ฐฐ์—ด A์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ด๋ผ๋ฉด 0๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
  2. ๋ฐฐ์—ด A์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ ํ›„ ์‚ญ์ œ
  3. ๋ฐฐ์—ด A์— ๋ณ€์ˆ˜ ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
    1. ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์—์„œ ์‚ญ์ œ
    2. ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ ์š”์†Œ๊ฐ€ ์—†๋‹ค๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋ฉˆ์ถ”๊ณ  ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜
  4. 2,3๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณต

 

Solution 1

public func solution(_ A : inout [Int]) -> Int {
    if A.count == 1{
        return A[0]
    }
    var temp = 0

    while true{
        temp = A.removeFirst()
        if A.contains(temp){
            A.remove(at: A.firstIndex(of: temp)!)
        }else{
            break
        }
    }
    return temp
}

 

  • ์ž…๋ ฅ์ด ํฐ ๊ฒฝ์šฐ timeout ์˜ค๋ฅ˜ ๋ฐœ์ƒ
  • ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N**2)

 

ํ•ด๊ฒฐ๋ฐฉ์•ˆ 2

  1. ๋ฐฐ์—ด A์˜ ์š”์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ temp ๋ณ€์ˆ˜์™€ XOR ์—ฐ์‚ฐํ•œ๋‹ค.
    • XOR ์—ฐ์‚ฐ์€ 2์ง„์ˆ˜ ๋‘ ๊ฐœ๋ฅผ ์—ฐ์‚ฐํ•˜์—ฌ ๊ฐ ์ž๋ฆฌ์˜ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด 0, ๋‹ค๋ฅด๋ฉด 1์„ ๋ฐ˜ํ™˜
    • ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” XOR ์—ฐ์‚ฐ์—์„œ 0์œผ๋กœ ๋ฌด์‹œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ง์„ ์ด๋ฃจ์ง€ ์•Š๋Š” ์›์†Œ๋งŒ ๋‚จ์Œ

Solution 2

public func solution(_ A : inout [Int]) -> Int {
    var temp = 0
    
    for item in A {
        temp ^= item
    }
    
    return temp
}

  • ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N) or O(N*log(N))
๋ฐ˜์‘ํ˜•

'โŒจ๏ธ Language > swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift] Codility Lesson 3 - PermMissingElem  (0) 2022.07.23
[Swift] Codility Lesson 3 - FrogJmp  (0) 2022.07.23
[Swift] Codility Lesson2 - CyclicRotation  (0) 2022.07.23
[Swift] Codility Lesson1 - BinaryGap  (0) 2022.07.23
[Swift] ์—ด๊ฑฐํ˜• (Enumerations)  (0) 2022.02.08