How to check if array is Palindrome ?

[Question ] Check weather array is palindrome Example : [1,2,2,1] ===> It’s palindrome
Solution#: Here we are checking first & last element equality & increase till mid of array

// Question# : Check Palindrome of array.
func checkPalindrome(inArray: inout [Int], startIndex: inout Int) ->Bool {
    if startIndex >= inArray.count/2 {return true}
    if inArray[startIndex] != inArray[inArray.count-startIndex-1] { return false}
    startIndex += 1
    return checkPalindrome(inArray: &inArray, startIndex: &startIndex)
}
var inArray3 = [1, 2, 2, 1]
var startIndex3 = 0
print(checkPalindrome(inArray: &inArray3, startIndex: &startIndex3))

Leave a Comment

Your email address will not be published. Required fields are marked *