Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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