Skip to content

Instantly share code, notes, and snippets.

View Piolli's full-sized avatar

Alexandr Piolli

View GitHub Profile
// 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)
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