I try to work out the functional implementation for the algorithm in leetcode (<https://leetcode.com/problems/two-sum/#/description>)

I guess what I need are 3 functions as followed:

* `List[Int] => List[(Int, Int)]` Zip the element with position
* `List[(Int, Int)], Int, Int => List[((Int, Int) , (Int, Int))]` Zip the element and position with rest of element and position and filter with Sum
* `List[((Int, Int) , (Int, Int))] => List[(Int, Int)]` Map to the position

The code looks like this:

```
  def findTwoSumElements(xs: List[Int], sum: Int): List[(Int, Int)] = {
    val zipWithIndex = xs.zipWithIndex
    var tailElements = zipWithIndex

    val result = zipWithIndex.map(t => {
      val element = zipAndFilter(tailElements, t, sum)
      tailElements = tailElements.tail
      element
     }
    )

    result.flatten.map(t => (t._1._2, t._2._2))
  }

  private def zipAndFilter(xs: List[(Int, Int)], x: (Int, Int), sum: Int): List[((Int, Int), (Int, Int))] = {
    xs.map(t => (t, x)).filter(t => (t._1._1 + t._2._1) == sum)
  }

 println(findTwoSumElements(List(1,2,3,4), 10)) //List()
 println(findTwoSumElements(List(1,2,3,4), 7)) //List((3,2))
 println(findTwoSumElements(List(1,2,3,4), 5)) //List((3,0), (2,1))
 println(findTwoSumElements(List(), 5)) //List()

```

I would like to improve this part of the code by not using `var`

```
  var tailElements = zipWithIndex

  val result = zipWithIndex.map(t => {
     val element = zipAndFilter(tailElements, t, sum)
     tailElements = tailElements.tail
     element
   }
  )

```

The reason is I want to remove duplicate Tuple[Int, Int], for example, it will return (x, y) and (y, x) together if I dont mutate the tail 

Can I have some suggestion or demo code about how to improve it and also for the whole implementation