In Go, partitioning a slice of integers involves rearranging the elements into two groups based on a pivot value, with one group containing elements less than or equal to the pivot, and the other containing elements greater than the pivot. Here's how to do it:
func partition(slice []int, pivot int) ([]int, []int) {
var less []int
var greater []int
for _, value := range slice {
if value <= pivot {
less = append(less, value)
} else {
greater = append(greater, value)
}
}
return less, greater
}
func main() {
nums := []int{8, 3, 5, 2, 10, 6}
pivot := 5
less, greater := partition(nums, pivot)
fmt.Println("Less than or equal to pivot:", less)
fmt.Println("Greater than pivot:", greater)
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?