/**
 * Given two lists, find the common elements between them using nested while loops.
 *
 * @param list1 The first list.
 * @param list2 The second list.
 * @return A list containing the common elements between list1 and list2.
 */
fun findCommonElements(list1: MutableList<Int>, list2: MutableList<Int>): MutableList<Int> {

    // Sort the lists
    list1.sort()
    list2.sort()

    // Initialize the variables and lists
    val commonElements = mutableListOf<Int>()
    var index1 = 0
    var index2 = 0

    // Use nested while loops to compare the elements
    while (index1 < list1.size && index2 < list2.size) {
        when {
            list1[index1] == list2[index2] -> {
                // Add the common element to the list
                commonElements.add(list1[index1])
                index1++
                index2++
            }
            list1[index1] < list2[index2] -> index1++
            else -> index2++
        }
    }

    return commonElements
}
/**
 * Given two lists, find the common elements between them using nested while loops.
 *
 * @param list1 The first list.
 * @param list2 The second list.
 * @return A list containing the common elements between list1 and list2.
 */
fun findCommonElements(list1: MutableList<Int>, list2: MutableList<Int>): MutableList<Int> {
/**
 * Given two lists, find the common elements between them using nested while loops.
 *
 * @param list1 The first list.
 * @param list2 The second list.
 * @return A list containing the common elements between list1 and list2.
 */
fun findCommonElements(list1: MutableList<Int>, list2: MutableList<Int>): MutableList<Int> {

    // Sort the lists
    list1.sort()
    list2.sort()

    // Initialize the variables and lists
    val commonElements = mutableListOf<Int>()
    var index1 = 0
    var index2 = 0

    // Use nested while loops to compare the elements
    while (index1 < list1.size && index2 < list2.size) {
        when {
            list1[index1] == list2[index2] -> {
                // Add the common element to the list
                commonElements.add(list1[index1])
                index1++
                index2++
            }
            list1[index1] < list2[index2] -> index1++
            else -> index2++
        }
    }

    return commonElements
}