DevPrep

All topics › Objective-C

Which operations are fast in NSSet vs NSArray?

Middle PRO

Essence

The difference is the data structure, not "iteration speed".

Iterating an array is usually slightly faster in practice (contiguous storage, cache locality), but asymptotically it is the same, and the interviewer wants "search in an array is linear, in a set it is constant".

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store