/**
 * Given two lists, find the intersection of the lists and modify one list with loops.
 *
 * @param list1 The first list.
 * @param list2 The second list.
 * @return The modified list1 with only the elements common to both lists.
 *
 * Example:
 * '``
 * val list1 = mutableListOf(1, 2, 3, 4, 5)
 * val list2 = mutableListOf(3, 4, 5, 6, 7)
 * println(intersectionCombinedLoopModifyingLists(list1, list2)) // Output: [3, 4, 5]
 * '``
 */
fun intersectionCombinedLoopModifyingLists(list1: MutableList<Int>, list2: List<Int>): List<Int> {

    // Find the intersection of the two lists
    val intersection = list1.filter { it in list2 }

    // Modify list1 with only the elements common to both lists
    list1.retainAll(intersection)

    return list1
}
/**
 * Given two lists, find the intersection of the lists and modify one list with loops.
 *
 * @param list1 The first list.
 * @param list2 The second list.
 * @return The modified list1 with only the elements common to both lists.
 *
 * Example:
 * '``
 * val list1 = mutableListOf(1, 2, 3, 4, 5)
 * val list2 = mutableListOf(3, 4, 5, 6, 7)
 * println(intersectionCombinedLoopModifyingLists(list1, list2)) // Output: [3, 4, 5]
 * '``
 */
fun intersectionCombinedLoopModifyingLists(list1: MutableList<Int>, list2: List<Int>): List<Int> {
/**
 * Given two lists, find the intersection of the lists and modify one list with loops.
 *
 * @param list1 The first list.
 * @param list2 The second list.
 * @return The modified list1 with only the elements common to both lists.
 *
 * Example:
 * '``
 * val list1 = mutableListOf(1, 2, 3, 4, 5)
 * val list2 = mutableListOf(3, 4, 5, 6, 7)
 * println(intersectionCombinedLoopModifyingLists(list1, list2)) // Output: [3, 4, 5]
 * '``
 */
fun intersectionCombinedLoopModifyingLists(list1: MutableList<Int>, list2: List<Int>): List<Int> {

    // Find the intersection of the two lists
    val intersection = list1.filter { it in list2 }

    // Modify list1 with only the elements common to both lists
    list1.retainAll(intersection)

    return list1
}