Home > Blockchain >  Change value in array in order
Change value in array in order

Time:05-09

I need to change maximum values to order. First in order will be maximum value and maximum value - 1. (100, 99, 100) must be equal to 1. It will be [5, 55, 1, 2, 1, 1, 98]. Then I need to change (98) to 2 because now 98 is maximum value in array. My goal is to have [4, 3, 1, 5, 1, 1, 2]. I tried my first steps and received this

var arr = [5, 55, 100, 2, 99, 100, 98]

func toOrder(_ arr: [Int]) -> [Int] {
  var arr = arr
  var max = arr.max()!
  var order = 1
  for i in arr.indices { 
    if arr[i] == max || arr[i] == max - 1 { 
       arr[i] = order
    }
  
  }
 return arr
}
toOrder(arr)
  

There i'm stuck. I received [5, 55, 1, 2, 1, 1, 98], but how can i continue to iterate through array with saved values?

CodePudding user response:

I have no idea what you should call this.

extension Sequence
where Element: AdditiveArithmetic & Comparable & ExpressibleByIntegerLiteral & Hashable {
  var            
  • Related