简体   繁体   中英

Unable to convert [Int] to integer in swift

I am having 5 arrayS. I removed duplicates from array. But, I unable to store new values in new array. I following the below method to convert in run time. The below logics will occur inside the loop. But, I showed my logic very detaily. I unable to convert integer array to int. So I tried to convert integer array to String. But failed. My coding is below.

var arr_1 = [Int]()
var arr_2 = [Int]()
var arr_3 = [Int]()
var arr_4 = [Int]()
var arr_5 = [Int]()
var arr_final = [Int]()

arr_1 = [2,2,2,2,2,2]
arr_2 = [3,3,3]
arr_3 = [5,5,5,5,5]
arr_4 = [1,1,1,1]
arr_5 = [0]

My Output to be:

arr_final = [2,3,5,1,0]

My Coding

var str_1 = String(stringInterpolationSegment: uniq(arr_1)).toInt()

arr_final.append(str_1!) //PRINTS FATAL ERROR


//REMOVE DUPLICATES
func uniq<S: SequenceType, E: Hashable where E==S.Generator.Element>(source: S) -> [E] {
    var seen: [E:Bool] = [:]
    return filter(source) { seen.updateValue(true, forKey: $0) == nil }
}

The function uniq returns always an array of Int .

To append an Array to an existing Array use extend rather than append

var arr_final = [Int]()

let arr_1 = [2,2,2,2,2,2]
let result = uniq(arr_1)
arr_final.extend(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM