Practice 50 Swift Generics Programming Questions, TechnoVlogs

Practice 50 Swift Generics Programming Questions


Q1. Write a Swift program to create a generic function that returns the first element of an array.
  Input: [1, 2, 3]
  Output: 1

Q2. Write a Swift program to create a generic function that swaps two elements of any type.
  Input: (a = 5, b = 10)
  Output: (a = 10, b = 5)

Q3. Write a Swift program to create a generic class that stores a pair of values.
  Input: Pair(10, "Hello")
  Output: Pair(10, "Hello")

Q4. Write a Swift program to create a generic function to print the elements of an array of any type.
  Input: ["apple", "banana"]
  Output: apple, banana

Q5. Write a Swift program to create a generic function to find the maximum value in an array of any comparable type.
  Input: [3, 1, 4]
  Output: 4

Q6. Write a Swift program to create a generic function that returns the middle element of an array.
  Input: [1, 2, 3]
  Output: 2

Q7. Write a Swift program to create a generic function that sums all the elements in an array of numbers.
  Input: [1, 2, 3]
  Output: 6

Q8. Write a Swift program to create a generic class to represent a stack and perform push and pop operations.
  Input: Stack.push(10), Stack.pop()
  Output: 10

Q9. Write a Swift program to create a generic function to check if two elements of any type are equal.
  Input: (5, 5)
  Output: true

Q10. Write a Swift program to create a generic function to print a dictionary of key-value pairs.
   Input: ["name": "Alice", "age": 25]
   Output: name: Alice, age: 25

Q11. Write a Swift program to create a generic function to find the index of an element in an array.
   Input: [10, 20, 30], Element: 20
   Output: 1

Q12. Write a Swift program to create a generic function to reverse an array.
   Input: [1, 2, 3]
   Output: [3, 2, 1]

Q13. Write a Swift program to create a generic function that returns the count of elements in an array.
   Input: [1, 2, 3]
   Output: 3

Q14. Write a Swift program to create a generic function that finds the smallest value in an array of comparable types.
   Input: [3, 1, 4]
   Output: 1

Q15. Write a Swift program to create a generic function that merges two arrays of any type.
   Input: [1, 2], [3, 4]
   Output: [1, 2, 3, 4]

Q16. Write a Swift program to create a generic function that finds the average of an array of numbers.
   Input: [2, 4, 6]
   Output: 4.0

Q17. Write a Swift program to create a generic function that removes the last element of an array.
   Input: [1, 2, 3]
   Output: [1, 2]

Q18. Write a Swift program to create a generic function that adds an element to an array.
   Input: [1, 2], Element: 3
   Output: [1, 2, 3]

Q19. Write a Swift program to create a generic class for a queue and implement enqueue and dequeue operations.
   Input: Queue.enqueue(5), Queue.dequeue()
   Output: 5

Q20. Write a Swift program to create a generic function to find if an array contains a specific element.
   Input: [1, 2, 3], Element: 2
   Output: true

Q21. Write a Swift program to create a generic function that finds the first index of an element in an array.
   Input: [10, 20, 30], Element: 20
   Output: 1

Q22. Write a Swift program to create a generic function that finds the last index of an element in an array.
   Input: [10, 20, 30, 20], Element: 20
   Output: 3

Q23. Write a Swift program to create a generic function that removes duplicates from an array.
   Input: [1, 2, 2, 3]
   Output: [1, 2, 3]

Q24. Write a Swift program to create a generic function that checks if an array is empty.
   Input: [1, 2, 3]
   Output: false

Q25. Write a Swift program to create a generic function that sorts an array of comparable types.
   Input: [3, 1, 2]
   Output: [1, 2, 3]

Q26. Write a Swift program to create a generic function to merge two dictionaries with the same key type and different value types.
   Input: ["a": 1], ["b": 2]
   Output: ["a": 1, "b": 2]

Q27. Write a Swift program to create a generic function to combine two sets into one.
   Input: [1, 2], [2, 3]
   Output: [1, 2, 3]

Q28. Write a Swift program to create a generic function to check if two sets are equal.
   Input: [1, 2], [1, 2]
   Output: true

Q29. Write a Swift program to create a generic function that finds the difference between two sets.
   Input: [1, 2], [2, 3]
   Output: [1]

Q30. Write a Swift program to create a generic function to find the union of two sets.
   Input: [1, 2], [2, 3]
   Output: [1, 2, 3]

Q31. Write a Swift program to create a generic function that finds the intersection of two sets.
   Input: [1, 2], [2, 3]
   Output: [2]

Q32. Write a Swift program to create a generic function that checks if a set is empty.
   Input: [1, 2, 3]
   Output: false

Q33. Write a Swift program to create a generic function that finds the symmetric difference of two sets.
   Input: [1, 2], [2, 3]
   Output: [1, 3]

Q34. Write a Swift program to create a generic function to filter elements from an array based on a condition.
   Input: [1, 2, 3], Condition: element > 1
   Output: [2, 3]

Q35. Write a Swift program to create a generic function to find the sum of a collection.
   Input: [1, 2, 3]
   Output: 6

Q36. Write a Swift program to create a generic function that transforms each element in an array.
   Input: [1, 2, 3], Transformation: element * 2
   Output: [2, 4, 6]

Q37. Write a Swift program to create a generic function that finds the element that appears the most in an array.
   Input: [1, 2, 2, 3, 3, 3]
   Output: 3

Q38. Write a Swift program to create a generic function to check if an element is contained in an array.
   Input: [1, 2, 3], Element: 2
   Output: true

Q39. Write a Swift program to create a generic function to flatten a nested array.
   Input: [[1, 2], [3, 4]]
   Output: [1, 2, 3, 4]

Q40. Write a Swift program to create a generic function to remove an element from an array.
   Input: [1, 2, 3], Element: 2
   Output: [1, 3]

Q41. Write a Swift program to create a generic function to combine two arrays of different types.
   Input: [1, 2], ["a", "b"]
   Output: [1, 2, "a", "b"]

Q42. Write a Swift program to create a generic function that maps an array of elements to a new array of transformed elements.
   Input: [1, 2, 3], Transformation: element * 2
   Output: [2, 4, 6]

Q43. Write a Swift program to create a generic function that checks if a dictionary contains a specific key.
   Input: ["name": "Alice"], Key: "name"
   Output: true

Q44. Write a Swift program to create a generic function that concatenates two arrays of the same type.
   Input: [1, 2], [3, 4]
   Output: [1, 2, 3, 4]

Q45. Write a Swift program to create a generic function to remove an element from a set.
   Input: [1, 2, 3], Element: 2
   Output: [1, 3]

Q46. Write a Swift program to create a generic function to remove an element from a dictionary by its key.
   Input: ["name": "John", "age": 25], Key: "age"
   Output: ["name": "John"]

Q47. Write a Swift program to create a generic function that converts an array of elements into a dictionary.
   Input: [("a", 1), ("b", 2)]
   Output: ["a": 1, "b": 2]

Q48. Write a Swift program to create a generic function that groups an array of elements into sub-arrays based on a condition.
   Input: [1, 2, 3], Condition: element % 2 == 0
   Output: [[2]]

Q49. Write a Swift program to create a generic function that creates an array from a dictionary's keys.
   Input: ["a": 1, "b": 2]
   Output: ["a", "b"]

Q50. Write a Swift program to create a generic function to find the common elements in two arrays.
   Input: [1, 2, 3], [2, 3, 4]
   Output: [2, 3]

Social Share

Bikki Singh Instructor TechnoVlogs

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!