
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to check if an Array is Palindrome or not
A palindrome array is an array that remains the same when its elements are reversed we can also say that the elements of a palindrome array remain the same even if we read it from backward or forward. For example, [1, 9, 10, 9, 1] is a palindrome array whereas [3, 5, 7, 8, 3, 2, 1] is not a palindrome array. In this article, we are going to learn different methods of finding whether the given array is a palindrome or not suing Swift programming language.
Algorithm
Step 1 − Create a function.
Step 2 − Reverse the elements of the array
Step 3 − Compare the array with the reversed array. If they are equal, then the given array is palindrome. Otherwise not.
Step 4 − Create an array and pass it to the function.
Step 5 − Print the output.
Example 1
Following the Swift program to check if an array is a palindrome or not.
import Foundation import Glibc // Function to check if the given array is palindrome or not func checkPalindrome(arr: [Int]) -> Bool { let reversedArray = Array(arr.reversed()) return arr == reversedArray } // Test Case 1 let mArr1 = [4, 2, 8, 7, 8, 2, 4] let result1 = checkPalindrome(arr: mArr1) print("Is Array - \(mArr1) is palindrome?:", result1) // Test Case 2 let mArr2 = [4, 1, 8, 7, 8, 6, 9] let result2 = checkPalindrome(arr: mArr2) print("Is Array - \(mArr2) is palindrome?:", result2)
Output
Is Array - [4, 2, 8, 7, 8, 2, 4] is palindrome?: true Is Array - [4, 1, 8, 7, 8, 6, 9] is palindrome?: false
Here, in the above code, we have two arrays of integer types. Now we create a function to check if an array is palindrome or not. In this function, first we reverse the array using reversed() function. Then compare reversed array with original array. If both the arrays are equal, then print “true”. If they are not equal to each other, then print “false”.
Example 2
Following Swift program to check if an array is palindrome or not.
import Foundation import Glibc // Function to check if the given array is palindrome or not func checkPalindrome(arr: [Int]) -> Bool { var start = 0 var end = arr.count - 1 while start < end { if arr[start] != arr[end] { return false } start += 1 end -= 1 } return true } // Test Case 1 let mArr1 = [9, 1, 8, 4, 8, 1, 9] let result1 = checkPalindrome(arr: mArr1) if result1 == true{ print("Given array is palindrome") } else { print("Given array is not a palindrome") } // Test Case 2 let mArr2 = [2, 1, 8, 7, 2] let result2 = checkPalindrome(arr: mArr2) if result2 == true{ print("Given array is palindrome") } else { print("Given array is not a palindrome") }
Output
Given array is palindrome Given array is not a palindrome
Here, in the above code, we have two arrays of integer types. Now we create a function to check if an array is palindrome or not. In this function, we initialise start = 0 and end = arr.count-1. Then while loop to iterate through each elements of the array and compare the starting and end elements of the array. If they are equal, then it return true that means the array is palindrome. If they are not equal to each other, then it return false that means the array is not palindrome.
Conclusion
In this article, we have used two different examples to help you understand different methods of finding whether the given array is a palindrome or not.
- Related Articles
- C Program to check if an Array is Palindrome or not
- C Program to check if an array is palindrome or not using Recursion
- Program to check if an Array is Palindrome or not using STL in C++
- C# program to check if a string is palindrome or not
- Python program to check if a string is palindrome or not
- Swift program to check if string is pangram or not
- Recursive program to check if number is palindrome or not in C++
- Swift Program to Check if an array is empty
- Write a C# program to check if a number is Palindrome or not
- Swift Program to check if a given string is Keyword or not
- Swift Program to Check If a Number is Spy number or not
- Queries to check if substring[L…R] is palindrome or not in C++ Program
- C++ Program to Check Whether a Number is Palindrome or Not
- Program to check a string is palindrome or not in Python
- Swift Program to Check if the given number is Perfect number or not
