Write a Scala program to remove the duplicate elements of a given sorted array and return the new length of the array
- برمجة سكالا
- 2021-09-24
- mhanasmh00489829403
الأجوبة
object Scala_Array {
def test(nums: Array[Int]) : Int = {
var index = 1;
for (i <- 0 to nums.length-1) {
if (nums(i) != nums(index))
{
index += 1
nums(index) = nums(i)
}
}
index;
}
def main(args: Array[String]): Unit = {
val nums = Array(20, 20, 30, 40, 50, 50, 50, 50, 60, 60);
println(s"Original array length: ${nums.length}");
println("Array elements are: ");
for (i <- 0 to nums.length - 1)
{
print(s"${nums(i)} ");
}
println(s"\nThe new length of the array after removing the duplicate elements is: ${test(nums)}");
}
}
Sample Output:
Original array length: 10 Array elements are: 20 20 30 40 50 50 50 50 60 60 The new length of the array after removing the duplicate elements is: 5
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال
معلومات ذات صلة