Question: Project Name: Array Unique Element Count Target Platform: Xcode Playground Programming Language: Swift For this project, create an Xcode playground. The playground, or a zip
Project Name: Array Unique Element Count Target Platform: Xcode Playground Programming Language: Swift
For this project, create an Xcode playground. The playground, or a zip file of the playground, is to be submitted.
The goal is to give the Array type a method called countUniqueElements that returns the number of unique elements in the array.
Lets say we have the following array:
let values = [-1, -4, 0, 4, 34, 5, 0, -1, -1, -4, 0, 5]
The array contains 6 unique values: 4, 1, 0, 4, 5, 34
If the countUniqueElements() method is called on the values array, it will return 6. In the code below, numUniqueElements is set to 6.
let numUniqueElements = values.countUniqueElements()
If an extension for Array is desired where there are no requirements for the elements in the array to have certain abilities, the extension block looks like the following. This would apply to all arrays regardless of the type of elements they contain.
extension Array { // code for extension goes here } If an Array extension is desired with a requirement for the elements in the array to have certain abilities as determined by the protocols the elements implement, the where clause is used to stipulate the requirement. For this challenge, an extension that is restricted to arrays that contain comparable elements is allowed, if needed. If the elements are comparable they can be compared using the comparison operators (<, >, <=, >= , ==) and sorted. For this case, the extension block looks like the following.
extension Array where Element: Comparable { // code for extension goes here // the elements of the array are comparable } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
