Advertisement
ProgNeo

Find Average Without Duplicates

Mar 3rd, 2023 (edited)
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.90 KB | None | 0 0
  1. import kotlin.random.Random
  2.  
  3. fun main() {
  4.     val numbers = Array(10) { Random.nextInt(1, 11) }
  5.     println("Исходные числа: ${numbers.contentToString()}")
  6.  
  7.     val (hasDuplicates, average) = findAverageWithoutDuplicates(numbers)
  8.  
  9.     if (hasDuplicates) {
  10.         println("Среди чисел есть повторяющиеся.")
  11.     } else {
  12.         println("Среди чисел нет повторяющихся.")
  13.     }
  14.  
  15.     println("Среднее арифметическое без учета повторов: $average")
  16. }
  17.  
  18. fun findAverageWithoutDuplicates(numbers: Array<Int>): Pair<Boolean, Double> {
  19.     val distinctNumbers = numbers.distinct()
  20.  
  21.     val hasDuplicates = numbers.size != distinctNumbers.size
  22.  
  23.     val sum = distinctNumbers.sum()
  24.     val count = distinctNumbers.size.toDouble()
  25.     val average = sum / count
  26.  
  27.     return Pair(hasDuplicates, average)
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement