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

Commit

Permalink
largest two elements (#5585)
Browse files Browse the repository at this point in the history
* Solves issue with swift not finding the largest two elements correctly

* Solves  #5560 Write a Scala program to convert temperature from celsius to kelvin

* Removes #5560 changes

* F# program to implement linear search (#5295)

---------

Co-authored-by: David Fernandez <david@Davids-MacBook-Pro.local>
Co-authored-by: Harsh Raj <harshraj8843@gmail.com>
  • Loading branch information
3 people authored Mar 28, 2024
1 parent d9293d3 commit 25f4991
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
func findLargestTwo(_ arr: [Int]) -> (Int, Int) {
var firstLargest = Int.min
var secondLargest = Int.min

for num in arr {
if num > firstLargest {
secondLargest = firstLargest
firstLargest = num
} else if num > secondLargest && num != firstLargest {
secondLargest = num
}
}

return (firstLargest, secondLargest)
}

// Test cases
let arr1 = [12, 13, 1, 10, 34, 1]
let arr2 = [10, 5, 10]
let arr3 = [10, 10, 10]

let result1 = findLargestTwo(arr1)
let result2 = findLargestTwo(arr2)
let result3 = findLargestTwo(arr3)

print("The largest two elements are \(result1.0) and \(result1.1)")
print("The largest two elements are \(result2.0) and \(result2.1)")
print("The largest two elements are \(result3.0) and \(result3.1)")

14 changes: 14 additions & 0 deletions program/program/implement-linear-search/ImplementLinearSearch.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let rec linearSearch (list : int list) (value : int) (index : int) : int option =
match list with
| [] -> None
| x::xs ->
if x = value then Some index
else linearSearch xs value (index + 1)

let searchValue = 4
let list = [1; 2; 3; 4; 5]

match linearSearch list searchValue 0 with
| Some index -> printfn "Output: %d" index
| None -> printfn "Value not found"

0 comments on commit 25f4991

Please sign in to comment.