Skip to content

Commit e1221c0

Browse files
committed
add single number III exercise
1 parent a715446 commit e1221c0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.sharpsw.leetcode
2+
3+
object SingleNumberIII {
4+
def singleNumber(nums: Array[Int]): Array[Int] = {
5+
nums.groupBy(l => l).mapValues(_.length).filter(p => p._2 == 1).keys.toArray
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.sharpsw.leetcode
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
5+
class SingleNumberIIISpec extends FlatSpec with Matchers {
6+
"Given nums = [1, 2, 1, 3, 2, 5]" should "return [3, 5]" in {
7+
val nums = Array[Int](1, 2, 1, 3, 2, 5)
8+
val result = SingleNumberIII.singleNumber(nums).sorted
9+
result should have length 2
10+
result should be (Array(3, 5))
11+
}
12+
}

0 commit comments

Comments
 (0)