Menu
Coddy logo textTech

Recap Challenge #1

Lesson 19 of 19 in Coddy's Functions and Pointers in Golang course.

Contains is a function defined within the slices package that checks whether a given element exists within a slice and returns a boolean value. It returns true if the element exists and false otherwise.

Here's an example demonstrating how this function works:

s := []int{1, 2, 3, 4, 5}
c := slices.Contains(s, 3)
fmt.Println(c)

In this example, we call the Contains function and pass two arguments. The first argument is the slice s that we want to check, and the second argument is the element we're searching for within the slice. In this case, we're looking for the value 3 within the slice s, which indeed exists, so the function returns true.

challenge icon

Challenge

Easy

Let's build our Contains function! 😇

Write a function called <strong>contain</strong> that takes a slice of type int and an element of type int.

Check if the element is present in the slice. If it exists, return <strong>true</strong>; if not, return <strong>false</strong>.

Try it yourself

// Your Code Here

All lessons in Functions and Pointers in Golang