swift - Eliminate repeating letters from Strings -


eliminate repeating letters strings in swift

var name1 : string =  "sandeep" var name2 : string = "warrior" var name3 : string = name1+name2 var name4 : string = //output should sndpwio 

in name 4, want eliminate repeating letters name 3.

how can achieve this?

name1 , name2 coming text box user.

use nscountedset count hold many times each character appears, filter appear once:

swift 3

let countedset = nscountedset(array: name3.characters.map { $0 }) let name4 = string(name3.characters.filter { countedset.count(for: $0) == 1 }) 

swift 4

in swift 4, string collection of characters again can shorten code this:

let countedset = nscountedset(array: name3.map { $0 }) let name4 = name3.filter { countedset.count(for: $0) == 1} 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -