-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
36 lines (28 loc) · 839 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Find the number occuring odd number of times in a given array of size 'n'
//Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times.
import Foundation
var arr = [3,4,5,3,4,5,5]
//Desired Output: 5
var output: Int = 0
//Method 1
//Do hashing , time complexity as O(n) : space complexity as O(n)
var dict :[String:Int] = [:]
for value in arr{
dict["\(value)"] = (dict["\(value)"] ?? 0) + 1
}
for key in dict.keys{
if let count = dict[key]{
if count%2 != 0{
print(key)
break
}
}
}
//Method 2 (Best solution)
//Do XOR , time complexity as O(n) : space complexity as O(1)
//NOTE: XOR of two same elements is zero , so will get the odd one.
output = 0
for value in arr{
output = output^value
}
print(output)