Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1346. Check If N and Its Double Exist #2

Open
kevin373738 opened this issue Oct 10, 2024 · 0 comments
Open

1346. Check If N and Its Double Exist #2

kevin373738 opened this issue Oct 10, 2024 · 0 comments

Comments

@kevin373738
Copy link

 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
      }
codedeman added a commit that referenced this issue Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant