- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Check if An Array Contains a Given Value
In this tutorial we will learn how to check if a given value is present in a Go−lang array or not. We will use 2 methods for this.
Method 1: Using a for loop directly in the main function
We will use a for loop to iterate over the elements of an array in Golang and check for the equality of values using the equality operator. If the values match then we can stop the iteration and say that the given value is present in the array.
Method 2: Using the function
Golang does not have any pre-defined library function method for this, so we need to create our own function.
Example 1: Golang Program to check if an Array Contains a Specific.
Syntax
var array_name [length] Type
In Golang to initialize an array, we need to specify three things. The first one is the name of the array followed by its size and then the data type of the value we wish to store in it.
For example, var number[10] int – this will initialize an array named number of integer data type and will a lot 10 memory at contiguous memory locations to store value in it.
Algorithm
Step 1 − Import the package fmt.
Step 2 − Start the function main().
Step 3 − Declare and Initialize an array, a variable of data type int and a boolean variable.
Step 4 − Iterate over the array using for loop.
Step 5 − Print the result on the screen using fmt.Printf().
Example
// GOLANG PROGRAM TO CHECK IF AN ARRAY CONTAINS A GIVEN VALUE package main import "fmt" // fmt package provides the function to print anything func main() { // initialize an array and store value in it array := [5]int {1, 2, 3, 4, 5} // initialize a variable of data type int var element int = 4 fmt.Println("The element to check = ",element) // initialize a datatype of Boolean and define it to false var result bool = false // iterate over the array using for loop and break if values match for i := 0; i < len(array); i++ { // checking if the array contains the given value if array[i] == element { // changing the boolean variable result = true break } } // printing the final result if result { fmt.Println("Array", array, "Contains the given Value", element) } else { fmt.Println("Array", array, "does not Contains the given Value", element) } }
Output
The element to check = 4 Array [1 2 3 4 5] Contains the given Value 4
Description of the Code
1. First, we import the package fmt that allows us to print anything.
2. Then we start the main() function.
3. Initialize an array of data type int and assign value to it.
4. Also initialize a variable to store the integer value that we wish to check.
5. Initialize a Boolean variable and initially store false in it.
6. Iterate over the array and use if condition to check wheather it contains the above initialized variable or not.
7. If it contains the variable flip the Boolean variable and break the loop.
8. Print the result on the screen.
Example 2: Golang Program to Check if an Array contains a Specific Element using Function
Syntax
func function_name([parameters]) [return_types]{ Body of the function }
Algorithm
Step 1 − Import the package fmt
Step 2 − Create the function isAvailable().
Step 3 − Start the function main().
Step 4 − Initialize the array of string
Step 5 − Call the function isAvailable().
Step 6 − Print the result on the screen using fmt.Printf().
Example
package main import "fmt" // defining the function with a parameter of string // type and have a return type bool func isAvailable(alpha []string, str string) bool { // iterate using the for loop for i := 0; i < len(alpha); i++ { // check if alpha[i] == str { // return true return true } } return false } // Start the function main() func main() { // Declare and initialize string datatype alpha := []string {"Akshay", "Emma", "David", "Akhil"} fmt.Println("Given List = ",alpha) // initializing a variable of data type string var toCheck string = "Akhil" fmt.Println("Value to search = ",toCheck) // calling the isAvailable() function result := isAvailable(alpha, toCheck) if result { fmt.Println(toCheck, "is present in the array of strings", alpha) } }
Output
Given List = [Akshay Emma David Akhil] Value to search = Akhil Akhil is present in the array of strings [Akshay Emma David Akhil]
Description of the Code
1. First, we have to import the fmt package that allows us to print anything on the screen.
2. Then we have to create a function that will take care of our logic.
3. We have created isAvailable() function that takes two arguments as strings one is the array of strings and another is the string that we wish to check. The function returns a Boolean value i.e either true or false.
4. In this loop we have used for loop to iterate over the array.
5. Then we have used the if conditionals to check whether the available string is present in the array or not.
6. If it is present then true and is returned. Otherwise this function will return false.
7. Start the main() function.
8. Initialize an array of strings and assign value to it.
9. Initialize a toCheck variable of data type string that will contain the value to be checked.
10. Call the isAvailable() function and pass the array of string and the toCheck variable as arguments to it.
11. Store the value returned by the function in a separate variable.
12. Print the result on the screen.
Conclusion
We have successfully compiled and executed the Golang program code to check if the element is present in the array. We have used 2 different methods to show this. First is direct method in which we have used a for loop to iterate over the array.
In the second method we have created a function and call it from the main function to check the same.
- Related Articles
- Java Program to Check if An Array Contains a Given Value
- Haskell Program to Check if An Array Contains a Given Value
- Java Program to Check if An Array Contains the Given Value
- Golang program to check if a string contains a substring
- How to check if a slice contains an element in Golang?
- How to check if a Perl array contains a particular value?
- Check if an array contains all elements of a given range in Python
- C# program to check if a value is in an array
- How to check if a vector contains a given value in R?
- Method to check if array element contains a false value in JavaScript?
- How to check if a string contains a substring in Golang?
- C# program to find if an array contains duplicate
- Golang Program to check a string contains a specified substring or not
- How to check if an array contains integer values in JavaScript ?
- Java Program to check if a given value exists in a HashMap
