Practice 50 Swift Collections Programming Questions
Q1. Write a Swift program to create an array of integers and print all elements in the array.
Input: [1, 2, 3, 4]
Output: 1, 2, 3, 4
Q2. Write a Swift program to find the length of an array of strings.
Input: ["apple", "banana", "cherry"]
Output: 3
Q3. Write a Swift program to add an element to an array of integers.
Input: [10, 20, 30], New Element: 40
Output: [10, 20, 30, 40]
Q4. Write a Swift program to remove an element from an array of strings.
Input: ["cat", "dog", "fish"], Remove: "dog"
Output: ["cat", "fish"]
Q5. Write a Swift program to check if an element exists in an array.
Input: [1, 2, 3, 4], Element: 3
Output: true
Q6. Write a Swift program to merge two arrays of integers.
Input: [1, 2], [3, 4]
Output: [1, 2, 3, 4]
Q7. Write a Swift program to find the index of an element in an array.
Input: ["apple", "banana", "cherry"], Element: "banana"
Output: 1
Q8. Write a Swift program to sort an array of integers in ascending order.
Input: [3, 1, 2]
Output: [1, 2, 3]
Q9. Write a Swift program to reverse an array of strings.
Input: ["a", "b", "c"]
Output: ["c", "b", "a"]
Q10. Write a Swift program to remove duplicates from an array of integers.
Input: [1, 2, 2, 3]
Output: [1, 2, 3]
Q11. Write a Swift program to create a dictionary with keys and values and print them.
Input: ["name": "John", "age": 30]
Output: name: John, age: 30
Q12. Write a Swift program to access a value from a dictionary using a key.
Input: ["name": "Alice", "city": "New York"], Key: "city"
Output: New York
Q13. Write a Swift program to add a key-value pair to a dictionary.
Input: ["name": "Bob"], Key: "age", Value: 25
Output: ["name": "Bob", "age": 25]
Q14. Write a Swift program to remove a key-value pair from a dictionary.
Input: ["name": "Charlie", "age": 30], Key: "age"
Output: ["name": "Charlie"]
Q15. Write a Swift program to check if a key exists in a dictionary.
Input: ["name": "Dave", "city": "Paris"], Key: "name"
Output: true
Q16. Write a Swift program to merge two dictionaries.
Input: ["a": 1], ["b": 2]
Output: ["a": 1, "b": 2]
Q17. Write a Swift program to find all keys in a dictionary.
Input: ["name": "Eve", "city": "Berlin"]
Output: ["name", "city"]
Q18. Write a Swift program to find all values in a dictionary.
Input: ["name": "Fay", "age": 28]
Output: ["Fay", 28]
Q19. Write a Swift program to create a set of integers and print the set.
Input: [1, 2, 3, 4]
Output: [1, 2, 3, 4]
Q20. Write a Swift program to add an element to a set.
Input: [1, 2, 3], New Element: 4
Output: [1, 2, 3, 4]
Q21. Write a Swift program to remove an element from a set.
Input: [1, 2, 3], Remove: 2
Output: [1, 3]
Q22. Write a Swift program to check if an element exists in a set.
Input: [1, 2, 3], Element: 3
Output: true
Q23. Write a Swift program to find the union of two sets.
Input: [1, 2], [2, 3]
Output: [1, 2, 3]
Q24. Write a Swift program to find the intersection of two sets.
Input: [1, 2], [2, 3]
Output: [2]
Q25. Write a Swift program to find the difference between two sets.
Input: [1, 2], [2, 3]
Output: [1]
Q26. Write a Swift program to convert a set to an array.
Input: [1, 2, 3]
Output: [1, 2, 3]
Q27. Write a Swift program to create an array of tuples and print it.
Input: [(1, "a"), (2, "b")]
Output: [(1, "a"), (2, "b")]
Q28. Write a Swift program to access elements of a tuple in an array.
Input: [(1, "a"), (2, "b")], Index: 1
Output: (2, "b")
Q29. Write a Swift program to find the length of a tuple.
Input: (1, 2, 3)
Output: 3
Q30. Write a Swift program to create a dictionary of tuples.
Input: ["name": (1, "Alice"), "age": (2, 30)]
Output: ["name": (1, "Alice"), "age": (2, 30)]
Q31. Write a Swift program to access a value in a tuple inside a dictionary.
Input: ["name": (1, "Bob")], Key: "name"
Output: (1, "Bob")
Q32. Write a Swift program to create a set of strings and print it.
Input: ["apple", "banana"]
Output: ["apple", "banana"]
Q33. Write a Swift program to remove duplicates from a set of strings.
Input: ["apple", "banana", "apple"]
Output: ["apple", "banana"]
Q34. Write a Swift program to sort an array of strings.
Input: ["pear", "apple", "banana"]
Output: ["apple", "banana", "pear"]
Q35. Write a Swift program to check if two arrays are equal.
Input: [1, 2, 3], [1, 2, 3]
Output: true
Q36. Write a Swift program to check if a dictionary is empty.
Input: ["name": "Tom"]
Output: false
Q37. Write a Swift program to find the first element of an array.
Input: [10, 20, 30]
Output: 10
Q38. Write a Swift program to find the last element of an array.
Input: [10, 20, 30]
Output: 30
Q39. Write a Swift program to create an empty dictionary.
Output: [:]
Q40. Write a Swift program to create an empty set of strings.
Output: []
Q41. Write a Swift program to find the maximum value in an array of integers.
Input: [3, 5, 1, 8]
Output: 8
Q42. Write a Swift program to find the minimum value in an array of integers.
Input: [3, 5, 1, 8]
Output: 1
Q43. Write a Swift program to iterate through an array and print each element.
Input: ["a", "b", "c"]
Output: a, b, c
Q44. Write a Swift program to join two arrays into one.
Input: [1, 2], [3, 4]
Output: [1, 2, 3, 4]
Q45. Write a Swift program to check if an array is empty.
Input: [1, 2]
Output: false
Q46. Write a Swift program to create an array from a set of integers.
Input: [1, 2, 3]
Output: [1, 2, 3]
Q47. Write a Swift program to find the common elements between two sets.
Input: [1, 2, 3], [2, 3, 4]
Output: [2, 3]
Q48. Write a Swift program to perform a symmetric difference of two sets.
Input: [1, 2], [2, 3]
Output: [1, 3]
Q49. Write a Swift program to filter even numbers from an array.
Input: [1, 2, 3, 4]
Output: [2, 4]
Q50. Write a Swift program to convert a dictionary into an array of tuples.
Input: ["name": "John", "age": 30]
Output: [("name", "John"), ("age", 30)]
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!