Harshit Kakkar
Harshit Kakkar

Reputation: 117

How to find duplicates in a list in Scala?

I have a list of unsorted integers and I want to find the elements which are duplicated.

val dup = List(1|1|1|2|3|4|5|5|6|100|101|101|102)

I have to find the list of unique elements and also how many times each element is repeated.

I know I can find it with below code :

val ans2 = dup.groupBy(identity).map(t => (t._1, t._2.size))

But I am not able to split the above list on "|" . I tried converting to a String then using split but I got the result below:

L
i
s
t
(
1
0
3
)

I am not sure why I am getting this result.

Reference: How to find duplicates in a list?

Upvotes: 1

Views: 2260

Answers (3)

themathmagician
themathmagician

Reputation: 505

Even easier, convert the list of duplicates into a set - a set is a data structure that by default does not have any duplicates.

scala> val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102)
dup: List[Int] = List(1, 1, 1, 2, 3, 4, 5, 5, 6, 100, 101, 101, 102)

scala> val noDup = dup.toSet
res0: scala.collection.immutable.Set[Int] = Set(101, 5, 1, 6, 102, 2, 3, 4, 100)

To count the elements, just call the method sizeon the resulting set:

scala> noDup.size
res3: Int = 9

Upvotes: 1

osleonard
osleonard

Reputation: 595

Another way to solve the problem

"1|1|1|2|3|4|5|5|6|100|101|101|102".split("\|").groupBy(x => x).mapValues(_.size)

res0: scala.collection.immutable.Map[String,Int] = Map(100 -> 1, 4 -> 1, 5 -> 2, 6 -> 1, 1 -> 3, 102 -> 1, 2 -> 1, 101 -> 2, 3 -> 1)

Upvotes: 0

SCouto
SCouto

Reputation: 7928

The symbol | is a function in scala. You can check the API here

|(x: Int): Int

Returns the bitwise OR of this value and x.

So you don't have a List, you have a single Integer (103) which is the result of operating | with all the integers in your pretended List.

Your code is fine, if you want to make a proper List you should separate its elements by commas

val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102)

If you want to convert your given String before having it on a List you can do:

"1|1|1|2|3|4|5|5|6|100|101|101|102".split("\\|").toList

Upvotes: 2

Related Questions