I solved this problem and was looking for feedback on my code. More efficient, cleaner, more verbose variable names, anything helps.
There are stringsN strings. Each string's length is no more than 20 characters. There are also Q queries. For each query, you are given a string, and you need to find out how many times this string occurred previously.
import Foundation
func readIntegers() -> Int {
return Int(readLine()!)!
}
func sparseArrays() {
let numOfStrings = readIntegers()
var arrayOfStrings = [String]()
for _ in 0..<numOfStrings {
let string = readLine()!
arrayOfStrings.append(string)
}
let numOfQueries = readIntegers()
for _ in 0..<numOfQueries {
let singleString = readLine()!
let arrayOfSingleString = arrayOfStrings.filter {$0 == singleString}
print(arrayOfSingleString.count)
}
}
sparseArrays()