Skip to content

Find the elements present in Array or not?

Code

func findSequence(_ list: [Int]) -> Bool {
    let sequence = [1, 2, 3]
        // Check if the sequence exists in the current list
        var foundSequence = true
        for element in sequence {
            if !list.contains(element) {
                foundSequence = false
                break
            }
        }
        if foundSequence {
            return true
        }
    return false
}

Example

// Example usage
let result = findSequence([3,4,6,2,1,3])
print(result)

Output

true