Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- There is no Swift syntax highlighting on pastebin yet :(
- Defining functions is pretty nasty. (variableName: valueType) (in this case, [Int] means an array containing integers)
- You also have to define the return type after the arrow. [Int : Bool] means the function will return a dictionary with
- integer keys and bool values. For example: {1 : true, 5 : false}
- */
- func arrayContains(search: [Int], match: [Int]) -> [Int : Bool] {
- var container = [Int : Bool]() //define a dictionary with integer keys and bool values, this dictionary will be returned
- for number in match { //iterate through the match array, set each number as a key in 'container' and set it to false
- container[number] = false
- }
- for element in search { //iterate through the array you're searching
- for compare in match { //also iterate through the comparison array so we can compare
- if compare == element { //if they match
- container[compare] = true //set the value of the key to true
- }
- }
- }
- return container
- }
- let test = arrayContains([5, 9], [5, 9, 10])
- println(test)
- /*
- 5: true (found in the search array)
- 9: true (found in the search array)
- 10: false (not found in the search array)
- */
Advertisement
Add Comment
Please, Sign In to add comment