Loading...
More Scala Posts
def reverseString(s: Array[Char]): Array[Char] = {for (i <- 0 until s.length / 2) {// Hint: Use your swap logic hereval temp = s(i)s(i) = s(s.length - 1 - i)s(s.length - 1 - i) = temp}s // Return the modified array}val result = reverseString(Array('H', 'a', 'n', 'n', 'a', 'h'))println(result.mkString(", ")) // Should print: h, a, n, n, a, H
def yourFunction(nums: Array[Int]): Boolean = {for(i <- 0 until nums.length) {for(j <- i + 1 until nums.length) {if (nums(i) == nums(j)) {return true // Return true only when a duplicate is found}}}false}// Your function hereval result1 = yourFunction(Array(1, 2, 3, 1))println(result1) // Should print: trueval result2 = yourFunction(Array(1, 2, 3, 4))println(result2) // Should print: falseval result3 = yourFunction(Array(1, 1, 1, 3, 3, 4, 3, 2, 4, 2))println(result3) // Should print: true
def twoSum(nums: Array[Int], target: Int): Array[Int] = {for(i <- 0 until nums.length) {for(j <- i + 1 until nums.length) {if(nums(i) + nums(j) == target) {return Array(i,j)}}}Array()}val result = twoSum(Array(3, 2, 4), 6)println(result.mkString(", ")) // Should print: 1, 2