NSDictionary Key For Value/Object?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
NSDictionary is optimized for key-to-value lookup, not reverse lookup from value to key. When you need a key for a given object, you either scan entries or maintain an additional reverse index. The right choice depends on data size, update frequency, and whether duplicate values are possible.
Direct Reverse Lookup by Enumeration
For occasional lookups on small dictionaries, iterate through keys and compare values.
This is simple and readable, but complexity is linear in dictionary size.
Handling Duplicate Values Correctly
Values in NSDictionary are not guaranteed unique. If several keys map to the same value, returning one key may not be enough.
Decide early whether your API should return first match or all matches.
Building a Reverse Index for Repeated Queries
If reverse lookup happens frequently, build a secondary map once.
This shifts work from query time to build time and is usually better for performance when lookups are frequent.
Swift Equivalent Pattern
In Swift, the same concept is concise and type-safe.
Use this approach when writing modern iOS codebases that mix Objective-C and Swift modules.
Equality and Hashing Considerations
Reverse lookup correctness depends on value equality semantics. For object values, isEqual: must reflect logical equality, and hash behavior should be consistent.
If your value objects are mutable, reverse indexes can become stale after mutation. Prefer immutable value objects for keys and indexed values where possible.
Designing API Behavior
For maintainable code, make reverse lookup API explicit:
keyForValue:returns first key or nil.keysForValue:returns all matching keys.
Document ordering expectations. Dictionary iteration order is not a sorting guarantee.
Choosing Between Scan and Index
For one-off lookups, scanning is often perfectly fine and keeps code simple. For repeated lookups inside loops or request handlers, building a reverse index usually pays off quickly.
A practical rule is to start with linear scan, then switch to reverse index when profiling shows lookup cost is meaningful. This prevents premature optimization while still giving a clear migration path as data grows.
If ordering matters for returned keys, sort the results explicitly before returning. Dictionary traversal order should not be treated as business ordering unless your application contract defines it.
Common Pitfalls
A common pitfall is assuming one-to-one mapping between keys and values. Reverse lookup logic breaks when duplicates appear unless this is handled explicitly.
Another issue is using pointer equality instead of value equality for object comparison. Use isEqual: to compare semantic value.
Developers also rebuild reverse indexes repeatedly inside loops. Build once and reuse if the source dictionary is stable.
Finally, mutating data after reverse index creation without refreshing the index leads to incorrect results. Keep update paths synchronized.
Summary
NSDictionarysupports fast key-to-value lookup, not value-to-key lookup.- Enumerate for occasional reverse lookups on small data.
- Build a reverse index for repeated queries.
- Handle duplicate values by returning multiple keys when needed.
- Ensure equality semantics and index refresh behavior are clearly defined.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.