/**
 * The exercise requires writing a function that takes in two lists of numbers,
 * and returns a new list containing the intersection of the two input lists.
 * The intersection of two lists is the common elements present in both lists.
 *
 * Your task is to complete the function 'intersectionCheck' below, using identity comparison
 * to compare the elements and append the common elements to the resulting list.
 */
fun intersectionCheck(numbers1: List<Int>, numbers2: List<Int>): List<Int> {

    val result = mutableListOf<Int>()

    // Check if any of the input lists are empty
    if (numbers1.isEmpty() || numbers2.isEmpty()) {
        return result
    }

    // Sort both lists in ascending order
    val sortedNumbers1 = numbers1.sorted()
    val sortedNumbers2 = numbers2.sorted()

    var index1 = 0
    var index2 = 0

    while (index1 < sortedNumbers1.size && index2 < sortedNumbers2.size) {
        // Compare the elements using identity comparison
        if (sortedNumbers1[index1] == sortedNumbers2[index2]) {
            // Append the common element to the resulting list
            result.add(sortedNumbers1[index1])
            index1++
            index2++
        } else if (sortedNumbers1[index1] < sortedNumbers2[index2]) {
            index1++
        } else {
            index2++
        }
    }

    return result
}
/**
 * The exercise requires writing a function that takes in two lists of numbers,
 * and returns a new list containing the intersection of the two input lists.
 * The intersection of two lists is the common elements present in both lists.
 *
 * Your task is to complete the function 'intersectionCheck' below, using identity comparison
 * to compare the elements and append the common elements to the resulting list.
 */
fun intersectionCheck(numbers1: List<Int>, numbers2: List<Int>): List<Int> {
/**
 * The exercise requires writing a function that takes in two lists of numbers,
 * and returns a new list containing the intersection of the two input lists.
 * The intersection of two lists is the common elements present in both lists.
 *
 * Your task is to complete the function 'intersectionCheck' below, using identity comparison
 * to compare the elements and append the common elements to the resulting list.
 */
fun intersectionCheck(numbers1: List<Int>, numbers2: List<Int>): List<Int> {

    val result = mutableListOf<Int>()

    // Check if any of the input lists are empty
    if (numbers1.isEmpty() || numbers2.isEmpty()) {
        return result
    }

    // Sort both lists in ascending order
    val sortedNumbers1 = numbers1.sorted()
    val sortedNumbers2 = numbers2.sorted()

    var index1 = 0
    var index2 = 0

    while (index1 < sortedNumbers1.size && index2 < sortedNumbers2.size) {
        // Compare the elements using identity comparison
        if (sortedNumbers1[index1] == sortedNumbers2[index2]) {
            // Append the common element to the resulting list
            result.add(sortedNumbers1[index1])
            index1++
            index2++
        } else if (sortedNumbers1[index1] < sortedNumbers2[index2]) {
            index1++
        } else {
            index2++
        }
    }

    return result
}