Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
harshraj8843 authored Jun 16, 2024
2 parents 38e6182 + 24fa08f commit 1420824
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation

func findSmallestThreeElements(arr: [Int]) -> [Int] {
guard arr.count >= 3 else {
print("Array should have at least 3 elements")
return []
}

var first = Int.max
var second = Int.max
var third = Int.max

for num in arr {
if num < first {
third = second
second = first
first = num
} else if num < second {
third = second
second = num
} else if num < third {
third = num
}
}

return [first, second, third]
}

// Example usage
let arr = [12, 13, 1, 10, 34, 1]
let smallestThree = findSmallestThreeElements(arr: arr)
print("The smallest three elements are: \(smallestThree)")

0 comments on commit 1420824

Please sign in to comment.