Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Objective-C Memory, Arrays and Loops, Oh My! Review NSArrays and NSMutableArrays

A repeating element in an Objective C array

What is the reason/usage for having the same element (i.e @"Bear") appear more than once in an NSArray or NSMutableArray?

2 Answers

An array can often hold multiple items of the same value. For instance you may want to store the results of coin flips in an array. This would mean that the same to values of would be repeated over and over again in the array. You could then iterate over the array to count the number of each result.

If you are looking for a data type that would only hold one copy of any given value you should instead look at using a set.

Hope that helps and let me know if I can explain anything further.

Could you give a production usage example?

The example I gave regarding coin flips that you may want to track is one example. Or keeping track of any user input that may repeat. Perhaps you were making a simple app that tracked foods that a person ate over a week. You just plug the foods into an array to keep track of them. If that person has the same food more than once you would have that food in the array twice. Really any information that might repeat. Does that help?

Yes, thanks