Skip to content

Commit 7405c64

Browse files
Priyanka-Priyanka-
Priyanka-
authored and
Priyanka-
committed
Kth Largest Element: updated to use Int.random API which has been introduced with Swift 4.2
1 parent 791cc66 commit 7405c64

File tree

1 file changed

+1
-7
lines changed

1 file changed

+1
-7
lines changed

Kth Largest Element/kthLargest.playground/Sources/kthLargest.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ public func kthLargest(_ a: [Int], _ k: Int) -> Int? {
1616

1717
// MARK: - Randomized selection
1818

19-
/* Returns a random integer in the range min...max, inclusive. */
20-
public func random(min: Int, max: Int) -> Int {
21-
assert(min < max)
22-
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
23-
}
24-
2519
/*
2620
Returns the i-th smallest element from the array.
2721

@@ -39,7 +33,7 @@ public func randomizedSelect<T: Comparable>(_ array: [T], order k: Int) -> T {
3933
var a = array
4034

4135
func randomPivot<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int) -> T {
42-
let pivotIndex = random(min: low, max: high)
36+
let pivotIndex = Int.random(in: low...high)
4337
a.swapAt(pivotIndex, high)
4438
return a[high]
4539
}

0 commit comments

Comments
 (0)