The String struct doesn’t provide a count or length property or method to count the number of characters it contains. Instead a global countElements<T>() function is available. When applied to strings, what’s the complexity of the countElements function:

O(1)

O(n)

and why?

Answer Posted / iosraj

Swift strings support extended grapheme clusters. Each character stored in a string is a sequence of one or more unicode scalars that, when combined, produce a single human readable character. Since different characters can require different amounts of memory, and considering that an extreme grapheme cluster must be accessed sequentially in order to determine which character it represents, it’s not possible to know the number of characters contained in a string upfront, without traversing the entire string. For that reason, the complexity of the countElements function is O(n).

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is singleton in swift?

452


What is memory leak in swift?

480


Why does apple use swift?

483


What do you do when you realize that your app is prone to crashing?

502


Is swift easier than java?

454






How will you define base class?

461


What is lazy stored procedure in swift and when is it used?

515


What are the different floating point numbers in swift?

472


Is swift similar to c?

432


Explain some biggest changes in usernotifications.

518


Is it worth learning swift 2019?

445


How many types of closures are there in swift?

427


What is an in-out parameter in swift?

515


Which is better car baleno or swift?

452


What is weak in swift?

518