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

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -