I wanted to write a Scala program that takes command-line args as list input and provide the output list without duplicates. I want to know the custom implementation of this without using any libraries.
Input : 4 3 7 2 8 4 2 7 3 Output :4 3 7 2 8
I wanted to write a Scala program that takes command-line args as list input and provide the output list without duplicates. I want to know the custom implementation of this without using any libraries.
Input : 4 3 7 2 8 4 2 7 3 Output :4 3 7 2 8
val x= List(4, 3, 7, 2, 8, 4, 2, 7, 3)
x.foldLeft(List[Int]())((l,v)=> if (l.contains(v)) l else v :: l)
if you can't use contains you can do another fold
x.foldLeft(List[Int]())((l,v)=> if (l.foldLeft(false)((contains,c)=>if (c==v ) contains | true else contains | false)) l else v :: l)
Here's a way you could do this using recursion. I've tried to lay it out in a way that's easiest to explain:
import scala.annotation.tailrec
@tailrec
def getIndividuals(in: List[Int], out: List[Int] = List.empty): List[Int] = {
if(in.isEmpty) out
else if(!out.contains(in.head)) getIndividuals(in.tail, out :+ in.head)
else getIndividuals(in.tail, out)
}
val list = List(1, 2, 3, 4, 5, 4, 3, 5, 6, 0, 7)
val list2 = List(1)
val list3 = List()
val list4 = List(3, 3, 3, 3)
getIndividuals(list) // List(1, 2, 3, 4, 5, 6, 0, 7)
getIndividuals(list2) // List(1)
getIndividuals(list3) // List()
getIndividuals(list4) // List(3)
This function takes two parameters, in
and out
, and iterates through every element in the in
List until it's empty (by calling itself with the tail
of in
). Once in
is empty, the function outputs the out
List.
If the out
List doesn't contain the value of in
you are currently looking at, the function calls itself with the tail of in
and with that value of in
added on to the end of the out
List.
If out
does contain the value of in
you are currently looking at, it just calls itself with the tail of in
and the current out
List.
Note: This is an alternative to the fold
method that Arnon proposed. I personally would write a function like mine and then maybe refactor it into a fold
function if necessary. I don't naturally think in a functional, fold
-y way so laying it out like this helps me picture what's going on as I'm trying to work out the logic.