0

I have following list

List(List
(43673,38448,512,36398,1500,**BpEwv+EcDv3z**,58f39535-03b7-4e05-a2d8-3f5b424c8938),
List(302750,759,512,759,3796,**BpEwv+EcDv3v**,069865df-30c3-48c3-bf02-79f2fcff7213),
List(616278,1600,512,107418,15255,**BpEwv+EcDv3v**,b373b731-6f38-4559-808e-1c05fc06af00),
List(0,0,512,0,0,**BpEwv+EcDv3z**,24894b9f-9e30-4073-a538-186a312c670e)
)

I want to remove duplicate values marked in bold (6th index of list for all elements) from this list. The sequence of elements is fixed.

Expected output:

List(
List(43673,38448,512,36398,1500,BpEwv+EcDv3z,58f39535-03b7-4e05-a2d8-3f5b424c8938),
List(302750,759,512,759,3796,BpEwv+EcDv3v,069865df-30c3-48c3-bf02-79f2fcff7213))

How do I remove duplicate values from list using scala??

12
  • Duplicate of this. Commented Nov 6, 2014 at 4:41
  • I want it to work slighlty different. Here I want to remove duplicates only on fields marked as ** and in blue color and also its a list of list Commented Nov 6, 2014 at 4:44
  • You want to remove duplicates or you want to remove all occurrences of a specific value? Commented Nov 6, 2014 at 5:03
  • Can you see my edit please??? I have list of list and want list by remove whole inner list if any specific value gets duplicated.. Commented Nov 6, 2014 at 5:20
  • 1
    Just a suggestion, it will be useful to give values like A, B instead of BpEwv+EcDv3z and BpEwv+EcDv3v which is different by only one character. It took quite a long time to understand what you want even after the expected solution that you posted. Commented Nov 6, 2014 at 6:11

2 Answers 2

2

If you want to remove all occurrences of a specific value in all Lists you can use the following code:

val lss = List(List(1,2,2), List(1,2,3,4,2))

lss map (_.filter(_ != 2))           //  List(List(1), List(1, 3, 4))

which removes all occurrences of 2 in all Lists.

If you want to get a single List in return you can use flatMap:

lss flatMap (_.filter(_ != 2))        // List(1, 1, 3, 4)
Sign up to request clarification or add additional context in comments.

Comments

1

Based on your expected output, you can do something like

scala> val a = List(List
 | (43673,38448,512,36398,1500,"BpEwv+EcDv3z","58f39535-03b7-4e05-a2d8-3f5b424c8938"),
 | List(302750,759,512,759,3796,"BpEwv+EcDv3v","069865df-30c3-48c3-bf02-79f2fcff7213"),
 | List(616278,1600,512,107418,15255,"BpEwv+EcDv3v","b373b731-6f38-4559-808e-1c05fc06af00"),
 | List(0,0,512,0,0,"BpEwv+EcDv3z","24894b9f-9e30-4073-a538-186a312c670e")
 | )
a: List[List[Any]] = List(List(43673, 38448, 512, 36398, 1500, BpEwv+EcDv3z, 58f39535-03b7-4e05-a2d8-3f5b424c8938), List(302750, 759, 512, 759, 3796, BpEwv+EcDv3v, 069865df-30c3-48c3-bf02-79f2fcff7213), List(616278, 1600, 512, 107418, 15255, BpEwv+EcDv3v, b373b731-6f38-4559-808e-1c05fc06af00), List(0, 0, 512, 0, 0, BpEwv+EcDv3z, 24894b9f-9e30-4073-a538-186a312c670e))

scala> a.groupBy(_(5)).mapValues(_(0)).map(_._2)
res0: scala.collection.immutable.Iterable[List[Any]] = List(List(302750, 759, 51
2, 759, 3796, BpEwv+EcDv3v, 069865df-30c3-48c3-bf02-79f2fcff7213), List(43673, 3
8448, 512, 36398, 1500, BpEwv+EcDv3z, 58f39535-03b7-4e05-a2d8-3f5b424c8938))

You can also do which reads a little better

scala> a.groupBy(_(5)).mapValues(_(0)).values.toList
res6: List[List[Any]] = List(List(302750, 759, 512, 759, 3796, BpEwv+EcDv3v, 069865df-30c3-48c3-bf02-79f2fcff7213), List(43673, 38448, 512, 36398, 1500, BpEwv+EcDv3z, 58f39535-03b7-4e05-a2d83f5b424c8938))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.