1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Harshit Kakkar
  • 117
  • 2
  • 12

3 Answers3

2

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
Community
  • 1
  • 1
SCouto
  • 7,808
  • 5
  • 32
  • 49
  • 1
    ok..Actually i faced a question that I have file having number separated by pipe and i need to remove duplicates and count the number of duplicates. so you are saying that it can not be done using scala ? – Harshit Kakkar May 29 '18 at 07:33
  • It can be done if your numbers are given as a String. List("1|1|1|2|3|4|5|5|6|100|101|101|102").map(_.split("\\|")) – SCouto May 29 '18 at 07:35
  • 1
    getting output as [Ljava.lang.String;@4769b07b – Harshit Kakkar May 29 '18 at 07:45
  • Sorry, i made a mistake, you should use a flatMap instead of a map. List("1|1|1|2|3|4|5|5|6|100|101|101|102").flatMap(_.split("\\|")).groupBy(identity).map(t => (t._1, t._2.size)) This is working for me – SCouto May 29 '18 at 07:48
1

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
themathmagician
  • 467
  • 5
  • 16
0

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)

osleonard
  • 595
  • 5
  • 22