Skip to content
Exercise · Final project · lesson 5 of 7

Exercise: merge sorted slices

Write a function func mergeSorted(a, b []int) []int that merges two slices sorted in ascending order into a new slice, also sorted in ascending order. The result holds every number from a and b, duplicates included.

  • mergeSorted([]int{1, 4, 6}, []int{2, 3, 7}) → [1 2 3 4 6 7]
  • mergeSorted([]int{1, 3, 3}, []int{3, 4}) → [1 3 3 3 4]
  • mergeSorted([]int{}, []int{2, 9}) → [2 9]

Constraints: each slice holds from 0 to 1000 numbers, which can include negative numbers and repeats; either slice can be empty, and for two empty slices the function returns an empty slice.

Do not add extra imports unless the task says so explicitly.

merge.go — go
go · 6 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way