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.

Updated on: 08-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements