Skip to content

1346. Check If N and Its Double Exist #2

Description

@kevin373738
 public func checkIfExist(_ arr: [Int]) -> Bool {
        guard !arr.isEmpty else { return false }
        var set: Set<Int>  = []
        for i in 0..<arr.count {
            
            if set.contains(arr[i] * 2) || (arr[i] % 2 == 0 && set.contains(arr[i] / 2)) {
                // If either the double or half of the current element exists in the set, return true
                return true
            }
            
            set.insert(arr[i])
            
        }
        
//        for i in 0..<arr.count {
//            let num = 2*arr[i]
//            results.insert(num)
//        }
//        for value in arr {
//            if results.contains(value) {
//                return true
//            }
//        }
        return false
      }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions