Skip to main content
shukurs0001
August 17, 2022
Question

How to find the max no from an array without using Max() ?

  • August 17, 2022
  • 9 replies
  • 0 views

Hi dear,

I have written code like in the picture, I just want to print the max no without using max function.

    9 replies

    August 17, 2022

    You can create a local variable of Map type and use to data subset to sort the array descending and get the 1st index value

    a!localVariables(
      local!listOfInts: {
        a!map(num: 5),
        a!map(num: 6),
        a!map(num: 2),
        a!map(num: 1),
        a!map(num: 10),
        a!map(num: 7)
      },
      index(
        index(
          todatasubset(
            local!listOfInts,
            a!pagingInfo(1, - 1, a!sortInfo("num", false()))
          ).data,
          "num",
          {}
        ),
        1,
        {}
      )
    )

    shukurs0001
    August 17, 2022

    Can't we achieve this by looping?

    August 17, 2022

    where you are looking for looping and why?

    harshitb6843
    August 17, 2022

    Hi [mention:c1f23620d448459fad309b37446c5d1f:e9ed411860ed4f2ba0265705b8793d05],

    I want to understand your use case. Why do you want to do it without the max() function? Tho there are multiple ways of doing it, max() will be the most efficient way (as Chris also mentioned) 

    mikes0011
    Brainy
    August 17, 2022

    max() is fairly useless in, let's say, the case of having an array of CDT and wanting to find the element in the array with the max value on a particular element - in which case, the "todatasubset" sorting method listed above works pretty well overall.

    The Expression Guru
    harshitb6843
    August 17, 2022

    Using max() with displayValue()?