this is the problem -
You are given an array of strings stored in the variable strings. Create a new array named countedStrings containing values of type (String,Int). Each tuple contains a string from the strings array followed by an integer indicating how many times it appears in the strings array. Each string should only appear once in the countedStrings array.
What I have tried:
I have not solved the problem as It is already solved. I am just trying to understand it. Some part of it i could understand and some parts I could not. Please any of you gentleman help me to understand the parts I could not understand. solved:-
var a = ["tuples", "are", "awesome", "tuples", "are", "cool",
"tuples", "tuples", "tuples", "shades"]
var y: [(String,Int)] = []///*[(String,Int)]: the type of your variable, in this case the type is Array of (String,Int), the compiler will allow you to insert only object of type (String,Int) in your Array. []: is default empty array.
for z in a{ // here z represents substring, a represents string
var x = false
for i in 0..<y.count {// i=0, i<y.count(because array starts with 0 and count starts with one
if (y[i].0 == z) {// could not understand
y[i].1 += 1// could not understand
x = true// could not understand
}
}
if x == false {// could not understand
let tuple = (z,2)// could not understand
y.append((z,1))// could not understand
}
}
print(y)
// prints [("tuples", 5), ("are", 2), ("awesome", 1), ("cool", 1), ("shades", 1)]