Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO CHECK IF AN ARRAY CONTAINS A GIVEN VALUE</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// fmt package provides the function to print anything</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// initialize an array and store value in it</span>
array <span class="token operator">:</span><span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token keyword">int</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
<span class="token comment">// initialize a variable of data type int</span>
<span class="token keyword">var</span> element <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">4</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The element to check = "</span><span class="token punctuation">,</span>element<span class="token punctuation">)</span>
<span class="token comment">// initialize a datatype of Boolean and define it to false</span>
<span class="token keyword">var</span> result bool <span class="token operator">=</span> <span class="token boolean">false</span>
<span class="token comment">// iterate over the array using for loop and break if values match</span>
<span class="token keyword">for</span> i <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
i <span class="token operator"><</span> <span class="token function">len</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span><span class="token punctuation">;</span>
i<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token comment">// checking if the array contains the given value</span>
<span class="token keyword">if</span> array<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> element <span class="token punctuation">{</span>
<span class="token comment">// changing the boolean variable</span>
result <span class="token operator">=</span> <span class="token boolean">true</span>
<span class="token keyword">break</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// printing the final result</span>
<span class="token keyword">if</span> result <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Array"</span><span class="token punctuation">,</span> array<span class="token punctuation">,</span> <span class="token string">"Contains the given Value"</span><span class="token punctuation">,</span> element<span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Array"</span><span class="token punctuation">,</span> array<span class="token punctuation">,</span> <span class="token string">"does not Contains the given Value"</span><span class="token punctuation">,</span> element<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// defining the function with a parameter of string</span>
<span class="token comment">// type and have a return type bool</span>
func <span class="token function">isAvailable</span><span class="token punctuation">(</span>alpha <span class="token punctuation">[</span><span class="token punctuation">]</span>string<span class="token punctuation">,</span> str string<span class="token punctuation">)</span> bool <span class="token punctuation">{</span>
<span class="token comment">// iterate using the for loop</span>
<span class="token keyword">for</span> i <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
i <span class="token operator"><</span> <span class="token function">len</span><span class="token punctuation">(</span>alpha<span class="token punctuation">)</span><span class="token punctuation">;</span>
i<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token comment">// check </span>
<span class="token keyword">if</span> alpha<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> str <span class="token punctuation">{</span>
<span class="token comment">// return true</span>
<span class="token keyword">return</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
<span class="token comment">// Start the function main()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Declare and initialize string datatype</span>
alpha <span class="token operator">:</span><span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>string <span class="token punctuation">{</span><span class="token string">"Akshay"</span><span class="token punctuation">,</span> <span class="token string">"Emma"</span><span class="token punctuation">,</span> <span class="token string">"David"</span><span class="token punctuation">,</span> <span class="token string">"Akhil"</span><span class="token punctuation">}</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Given List = "</span><span class="token punctuation">,</span>alpha<span class="token punctuation">)</span>
<span class="token comment">// initializing a variable of data type string</span>
<span class="token keyword">var</span> toCheck string <span class="token operator">=</span> <span class="token string">"Akhil"</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Value to search = "</span><span class="token punctuation">,</span>toCheck<span class="token punctuation">)</span>
<span class="token comment">// calling the isAvailable() function</span>
result <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">isAvailable</span><span class="token punctuation">(</span>alpha<span class="token punctuation">,</span> toCheck<span class="token punctuation">)</span>
<span class="token keyword">if</span> result <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span>toCheck<span class="token punctuation">,</span> <span class="token string">"is present in the array of strings"</span><span class="token punctuation">,</span> alpha<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
