/**
 * Write a function that takes two lists as input and returns a new list containing the common elements present in both lists.
 *
 * Example:
 * intersectionOfLists(listOf(1, 2, 3, 4, 5), listOf(4, 5, 6, 7, 8)) -> [4, 5]
 *
 * @param list1 The first list
 * @param list2 The second list
 * @return A new list containing the common elements found in both input lists
 *
 * Note:
 * - The order of the elements in the output list should be the same as the order in which they appear in the first list
 * - An element should only be included once in the output list, even if it appears multiple times in any of the input lists
 * - You cannot use any built-in Kotlin functions such as intersect(), intersectAll(), or any list operations to solve this exercise
 */
fun intersectionOfLists(list1: List<Int>, list2: List<Int>): List<Int> {

    val commonElements = mutableListOf<Int>()

    // Loop through each element in list1
    for (element in list1) {
        if (element in list2 && element !in commonElements) {
            commonElements.add(element)
        }
    }

    return commonElements
}
/**
 * Write a function that takes two lists as input and returns a new list containing the common elements present in both lists.
 *
 * Example:
 * intersectionOfLists(listOf(1, 2, 3, 4, 5), listOf(4, 5, 6, 7, 8)) -> [4, 5]
 *
 * @param list1 The first list
 * @param list2 The second list
 * @return A new list containing the common elements found in both input lists
 *
 * Note:
 * - The order of the elements in the output list should be the same as the order in which they appear in the first list
 * - An element should only be included once in the output list, even if it appears multiple times in any of the input lists
 * - You cannot use any built-in Kotlin functions such as intersect(), intersectAll(), or any list operations to solve this exercise
 */
fun intersectionOfLists(list1: List<Int>, list2: List<Int>): List<Int> {
/**
 * Write a function that takes two lists as input and returns a new list containing the common elements present in both lists.
 *
 * Example:
 * intersectionOfLists(listOf(1, 2, 3, 4, 5), listOf(4, 5, 6, 7, 8)) -> [4, 5]
 *
 * @param list1 The first list
 * @param list2 The second list
 * @return A new list containing the common elements found in both input lists
 *
 * Note:
 * - The order of the elements in the output list should be the same as the order in which they appear in the first list
 * - An element should only be included once in the output list, even if it appears multiple times in any of the input lists
 * - You cannot use any built-in Kotlin functions such as intersect(), intersectAll(), or any list operations to solve this exercise
 */
fun intersectionOfLists(list1: List<Int>, list2: List<Int>): List<Int> {

    val commonElements = mutableListOf<Int>()

    // Loop through each element in list1
    for (element in list1) {
        if (element in list2 && element !in commonElements) {
            commonElements.add(element)
        }
    }

    return commonElements
}