Lonely Integer

  • + 0 comments

    Wrote this solution by Swift, not perfect about alghoritms anyone can improve this for any suggest?

    func lonelyInteger(arr : [Int]) -> Int{
        
        var checkinNum = -1
        var count = 0
        var res = 0
        
        var sorted = arr.sorted(by: <)
        var uniqArr = Array(Set(sorted))
        
        uniqArr.forEach { num in
            checkinNum = num
            count = 0
            sorted.filter { filterNum in
                if checkinNum == filterNum {
                    count += 1
                    return true
                }
                else{
                    return false
                }
            }
            if count == 2{
                checkinNum = num
            }
            else{
                res = num
            }
        }
        return res
    }