LoginSignup
0
0

More than 5 years have passed since last update.

連続な重複番号を除く

Posted at

// input
val xs = (1, "aaa")::(1, "bbb")::(1, "ccc")::(2, "abc")::(3, "xxx")::(3, "kkk")::(1, "abcde")::(1, "qwee")::Nil

// output
val expect = List((1, "aaa"), (2, "abc"), (3, "xxx"), (1, "abcde"))

// fold
val resultFold = xs.foldLeft(List[(Int, String)]()){ 
               case (Nil, t) => List(t)
               case (ys@((i, s)::_), t@(j, q)) => if(i == j) ys else t::ys 
           }.reverse
assert(expect == resultFold)

// invert
val resultInvert = xs.head :: (xs, xs.tail).invert.collect{ case ((i, x), (j, y)) if i != j => (j, y) }
assert(expect == resultInvert )
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0