/**
 * Find the intersection of two lists using while loop and equality check.
 *
 * @param nums1 A list of integers.
 * @param nums2 A list of integers.
 * @return A list of integers representing the intersection of nums1 and nums2.
 */
fun intersectionWhileEqual(nums1: List<Int>, nums2: List<Int>): List<Int> {

    // Sort the lists
    val sortedNums1 = nums1.sorted()
    val sortedNums2 = nums2.sorted()
    val intersection = mutableListOf<Int>()

    var i = 0
    var j = 0

    // While both i and j are within the list bounds
    while (i < sortedNums1.size && j < sortedNums2.size) {
        // If the current elements are equal, add it to the intersection list
        if (sortedNums1[i] == sortedNums2[j]) {
            intersection.add(sortedNums1[i])
            i++
            j++
        } else if (sortedNums1[i] < sortedNums2[j]) {
            // If the element in nums1 is smaller, move to the next element
            i++
        } else {
            // If the element in nums2 is smaller, move to the next element
            j++
        }
    }

    return intersection
}
/**
 * Find the intersection of two lists using while loop and equality check.
 *
 * @param nums1 A list of integers.
 * @param nums2 A list of integers.
 * @return A list of integers representing the intersection of nums1 and nums2.
 */
fun intersectionWhileEqual(nums1: List<Int>, nums2: List<Int>): List<Int> {
/**
 * Find the intersection of two lists using while loop and equality check.
 *
 * @param nums1 A list of integers.
 * @param nums2 A list of integers.
 * @return A list of integers representing the intersection of nums1 and nums2.
 */
fun intersectionWhileEqual(nums1: List<Int>, nums2: List<Int>): List<Int> {

    // Sort the lists
    val sortedNums1 = nums1.sorted()
    val sortedNums2 = nums2.sorted()
    val intersection = mutableListOf<Int>()

    var i = 0
    var j = 0

    // While both i and j are within the list bounds
    while (i < sortedNums1.size && j < sortedNums2.size) {
        // If the current elements are equal, add it to the intersection list
        if (sortedNums1[i] == sortedNums2[j]) {
            intersection.add(sortedNums1[i])
            i++
            j++
        } else if (sortedNums1[i] < sortedNums2[j]) {
            // If the element in nums1 is smaller, move to the next element
            i++
        } else {
            // If the element in nums2 is smaller, move to the next element
            j++
        }
    }

    return intersection
}