Skip to content

Instantly share code, notes, and snippets.

@lingoslinger
Last active September 15, 2023 23:40
Show Gist options
  • Save lingoslinger/e81f28e8217617004839f89e174e173a to your computer and use it in GitHub Desktop.
Save lingoslinger/e81f28e8217617004839f89e174e173a to your computer and use it in GitHub Desktop.
Swift: Find second smallest and second largest elements in an array of integers
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
// Implement a function in Swift that finds the second smallest and second largest elements in an array of integers.
// this is a trick question designed to make you waste time if you haven't memorized Foundation...
func seconds(_ numbers: [Int]) -> (Int?, Int?){
let sorted = numbers.sorted()
return (sorted.dropFirst().first, sorted.dropLast().last )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment