Skip to content

Commit d91e76c

Browse files
Day 1177 solved in swift
1 parent 89fe991 commit d91e76c

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Swift/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Given an array of integers where every integer occurs three times except for one integer,
2+
// which only occurs once, find and return the non-duplicated integer.
3+
4+
// For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19.
5+
6+
func findSingleOccurrenceItem(myList: [Int]) -> Int? {
7+
var occurrenceCountMap : [Int: Int] = [:]
8+
myList.forEach { num in
9+
occurrenceCountMap[num] = (occurrenceCountMap[num] ?? 0) + 1
10+
}
11+
for (key, value) in occurrenceCountMap {
12+
if value == 1 {
13+
return key
14+
}
15+
}
16+
return nil
17+
}
18+
19+
var givenList :[Int] = [6, 1, 3, 3, 3, 6, 6]
20+
21+
var result : Int? = findSingleOccurrenceItem(myList: givenList)
22+
23+
if result == nil {
24+
print("No single occurrence item found")
25+
} else {
26+
print("\(result!) is the single occurrence item in the list")
27+
}

Swift/DailyCodingProblemsWithSwift.playground/contents.xcplayground

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<page name='BaseballGame'/>
1515
<page name='Day1093'/>
1616
<page name='Day1090'/>
17+
<page name='Day1177'/>
1718
</pages>
1819
</playground>

0 commit comments

Comments
 (0)