
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Search an Element in an Array using Swift
In this article, we will learn how to write a swift program to search an element in an Array. Here we use the following methods to search elements from the given array: To search
Using == operator
Using contains() method
Using the in function
Method 1: Using == operator
To search for an element from the given array we iterate through each element of the given array and check if the element is equal to the specified element using the == operator. If true, then the specified element is present in the array. Otherwise not.
Algorithm
Step 1 ? Create a function.
Step 2 ? In this function, use a for loop to iterate through each element of the array.
Step 3 ? Check if the current element is equal to the specified element or not using == operator.
Step 4 ? If the specified element is found in the array, then this function return true. Otherwise return false.
Step 5 ? Create an array of integer type.
Step 6 ? Call the function and pass the array and the element which we want to search as parameters in it.
Step 7 ? Print the output.
Example
Following Swift program to search an element in an Array
import Foundation import Glibc // Function to search specified element in the array func searchElement(array: [Int], element: Int) -> Bool { for ele in array { if ele == element { return true } } return false } // Test case let arr = [1, 2, 3, 4, 5] print("Is 4 present in the given array?", searchElement(array: arr, element: 4)) print("Is 10 present in the given array?", searchElement(array: arr, element: 10))
Output
Is 4 present in the given array? true Is 10 present in the given array? false
Here in the above code, we have an array of integer types. Now we create a function to check if the given element is present in the array or not. So we iterate through each element of the array and check whether the current element is equal to the specified element or not. If the element is found in the given array, then the loop ends and this function returns true. Otherwise, the checking process continues until the end of the array, and this function returns false.
Method 2: Using contains() function
To search for an element from the given array we can also use contains() function. This function is used to check if the specified element present in the given array or not. If it returns true, which means the specified element is present in the array. Alternatively, if it returns false, which means the specified element is not present in the array
Algorithm
Step 1 ? Create a function.
Step 2 ? In this function, use contains the () function to check if the current element is equal to the specified element or not.
Step 3 ? Create an array of integer types.
Step 4 ? Call the function and pass the array and the element, which we want to search as parameters in it.
Step 5 ? Print the output.
Example
Following Swift program to search an element in an Array
import Foundation import Glibc // Function to search specified element in the array func searchElement(array: [Int], ele: Int) -> Bool { return array.contains(ele) } // Test case let arr = [34, 5, 67, 32, 4, 56, 6, 54, 3] print("Is 32 present in the given array?", searchElement(array: arr, ele: 32)) print("Is 11 present in the given array?", searchElement(array: arr, ele: 11))
Output
Is 32 present in the given array? true Is 11 present in the given array? false
Here in the above code, we have an array of integer types. Now we create a function to check if the given element is present in the array or not. So we use contains the () function to check if the specified element is present in the given array or not. The contains() function will return true if the element is found in the array. Otherwise, it will return false.
Method 3: Using Function
To search an element from the given array we can also a user define function in which we check if the given element is present in the array or not.
Example
Following Swift program to search an element in an Array
import Foundation import Glibc // Function to search specified element in the array func searchElement(array: [Int], ele: Int) -> Bool { var foundele = false var i = 0 while !foundele && i < array.count { if array[i] == ele { foundele = true } else { i += 1 } } return foundele } // Test case let array = [34, 2, 56, 3, 56, 7, 88] print("Is element = 4 is present in the array?", searchElement(array: array, ele: 4)) print("Is element = 88 is present in the array?", searchElement(array: array, ele: 88))
Output
Is element = 4 is present in the array? false Is element = 88 is present in the array? true
Here in the above code, searchElement function uses a while loop to iterate through the elements of the array. It uses a found flag to track whether the specified element has been found. If the specified element is found, the flag is set to true and the loop is terminated. If the specified element is not found, then the loop continues until the end of the array is reached. The function then returns the value of the found flag.
Conclusion
Therefore, this is how we can search an element in an Array using the discussed methods. All the methods are applicable for any data type (with some minor changes).