This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Time: O(n) | |
// Space: O(n) | |
func findOne(in nums: [Int], duplicatesCount: Int) -> Int { | |
return (duplicatesCount * Set(nums).reduce(0, +) - nums.reduce(0, +)) / (duplicatesCount - 1) | |
} | |
assert(findOne(in: [1, 2, 3, 1, 2, 3, 1, 2, 3, 44, 1, 2, 3], duplicatesCount: 4) == 44) | |
assert(findOne(in: [1, 2, 1, 2, 55, 1, 2], duplicatesCount: 3) == 55) | |
assert(findOne(in: [1, 2, 25, 1, 2], duplicatesCount: 2) == 25) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func swapXOR<T: AnyObject>(object1: inout T, object2: inout T) { | |
// get pointers to objects | |
let pointer1 = Unmanaged.passUnretained(object1).toOpaque() | |
let pointer2 = Unmanaged.passUnretained(object2).toOpaque() | |
// get addresses from pointers | |
var a = UInt(bitPattern: pointer1) | |
var b = UInt(bitPattern: pointer2) | |
// swap addresses by XOR |