Home > Software engineering >  Scala TwoSum can't get Array[Int]
Scala TwoSum can't get Array[Int]

Time:02-15

can you please halp me with my solution. I write some method it get some Array[Int], and target and find summ

Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0]   nums[1] == 9, we return [0, 1].

def twoSum(nums: Array[Int], target: Int) : Array[Int] = {  
      val numSorted = nums.sorted
      for (i <- 0 until numSorted.length) {
        for (j <- i   1 until numSorted.length) {
          val l = numSorted(i)   numSorted(j)
          if (l == target) {
             Array(nums.indexOf(numSorted(i)), nums.indexOf(numSorted(j)))
          }
        }
      }
    }
    val nums = Array(2, 7, 11, 15)
    val target = 9
    twoSum(nums,target)

my code is worked without method twoSum , but when I put it into method it get ERROR

type mismatch;
 found   : Unit
 required: Array[Int]

CodePudding user response:

Well, your function isn't returning anything, as explained in the comments (but using return is considered a bad practice in scala, so, I would advice against that).

I also wanted to mention, that it's not a very good solution, because it is quadratic (the nested loop makes it very slow when the array is large)... Also, it doesn't look very much like scala code I am afraid.

I would suggest something like this:

     def twoSum(nums: Array[Int], target: Int) = {
        val diffs = nums.map(target - _).zipWithIndex.toMap
        nums.zipWithIndex.collectFirst { 
            case (n, i) if (diffs.get(n).nonEmpty) => Array(i, diffs(n))
        }.getOrElse(Array.empty)
     }

First, this creates a hash map of every possible difference between the target and an array element to the index of that element. Then it scans the array looking for the the first number that appears in the map: if the key in the map equals to the current element, then the sum of this element and the one at the index, pointed to by the mapped value equals the target.

I recommend that you try running each of these statements separately ini REPL and seeing what exactly they do.

CodePudding user response:

In Scala the last line in a function is what is returned. Typically in other languages this is return x where as in Scala it can just be x without the return keyword.

In your code snippet there is no return value so Scala infers the type to be Unit which does not match up with your return type Array[Int] so there is a type mismatch as reported by the compiler.

Here's your code with @Luis suggestions in comments to address the issue.

def twoSum(nums: Array[Int], target: Int) : Array[Int] = {  
      val numSorted = nums.sorted
      for (i <- 0 until numSorted.length) {
        for (j <- i   1 until numSorted.length) {
          val l = numSorted(i)   numSorted(j)
          if (l == target) {
            return Array(nums.indexOf(numSorted(i)), nums.indexOf(numSorted(j)))
          }
        }
      }
      return Array.empty
    }

It now type checks and runs OK.

scala> :load twoSum.scala
def twoSum(nums: Array[Int], target: Int): Array[Int]
                                                                                                                              
scala> twoSum(Array(2,7,11,15), 9)
val res0: Array[Int] = Array(0, 1)
  • Related