how to compare an element of array with all other elements in swift

You can compare an element of an array with all the other elements by iterating through the array using a loop and comparing the element at the desired index with every other element in the array. Here's an example in Swift:

main.swift
let arr = [1, 2, 3, 4, 5]
let indexToCompare = 2

for i in 0..<arr.count {
    if i != indexToCompare {
        if arr[indexToCompare] == arr[i] {
            print("Element at index \(indexToCompare) is equal to element at index \(i)")
        }
    }
}
255 chars
11 lines

related categories

gistlibby LogSnag