Skip to content

Commit

Permalink
[Array] replace for loop with fast enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi Gu committed Jun 11, 2016
1 parent 9788095 commit ba88116
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Array/RemoveDuplicatesFromSortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class RemoveDuplicatesFromSortedArray {

var lastIndex = 0

for i in 1 ..< nums.count {
if nums[i] != nums[lastIndex] {
for num in nums {
if num != nums[lastIndex] {
lastIndex += 1
nums[lastIndex] = nums[i]
nums[lastIndex] = num
}
}

Expand Down
6 changes: 3 additions & 3 deletions Array/RemoveElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class RemoveElement {
func removeElement(inout nums: [Int], _ val: Int) -> Int {
var lastIndex = 0

for i in 0 ..< nums.count {
if nums[i] != val {
nums[lastIndex] = nums[i]
for num in nums {
if num != val {
nums[lastIndex] = num
lastIndex += 1
}
}
Expand Down
6 changes: 3 additions & 3 deletions Array/TopKFrequentElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class TopKFrequentElements {
map[num] = times + 1
}

let keys = Array(map.keys)
var sortedKeys = keys.sort() {
var keys = Array(map.keys)
keys.sortInPlace() {
let value1 = map[$0]
let value2 = map[$1]
return value1 > value2
}

return Array(sortedKeys[0..<k])
return Array(keys[0 ..< k])
}
}

0 comments on commit ba88116

Please sign in to comment.