Skip to main content
December 29, 2022
Question

To find the greatest number in the array without max function

  • December 29, 2022
  • 11 replies
  • 0 views

conventional method to find the gratest number using for loop 

11 replies

harshitb6843
December 29, 2022

Why do you want to do it without the max() function when you can use it?

December 29, 2022

was practicing to do same as in c , using a local temp variable 

December 29, 2022

reduce(
  rule!getMaxNumber(accumulator: _, item: _),
  0,
  ri!array
)

We can use reduce function

Create a rule which will compare the current item and accumulator and return the max number and use that rule as predicate in reduce fucntion

alexandru.boerescu
December 30, 2022

Or use the sort function:

a!localVariables(
  local!list:{5,4,7,9,2,5,0},
  sort(local!list)[length(local!list)]
)

harshitb6843
January 14, 2024

sort is actually an undocumented function. This neither comes up as a suggestion nor there is any documentation page for the same. 
I won't advise using it. 

January 1, 2024

This might be helpful.

a!localVariables(
  local!listOfNumber: { 67, 80, 100, 102, 150 },
  a!forEach(
    items: local!listOfNumber,
    expression: a!localVariables(
      local!item: fv!item,
      local!updatedList: remove(local!listOfNumber, fv!index),
      if(
        count(
          wherecontains(
            true,
            a!forEach(
              items: local!updatedList,
              expression: local!item > fv!item
            )
          )
        ) = count(local!updatedList),
        fv!item,
        {}
      )
    )
  )
)

amans0009
January 5, 2024

Here is an O(n) approach implemented like C, employing a temporary variable to store the current maximum (rule input ). In this implementation, a recursive approach has been utilized. If you have any questions or need further clarification, feel free to ask.


  if(
    ri!currentIndex > length(ri!arr),
    ri!currentMax,
    rule!maxNum(
      arr: ri!arr,
      currentMax: if(
        ri!currentMax > ri!arr[ri!currentIndex],
        ri!currentMax,
        ri!arr[ri!currentIndex]
      ),
      currentIndex: ri!currentIndex + 1
    )
  )


/*arr: array in which we needs to find max num*/
/*currentMax: first element of array*/
/*current index: first index of array (1 in case of appian)*/

result-> 

mikes0011
Brainy
January 5, 2024

I'd point out that you're not actually declaring any local variable in this rule, and thus it's unnecessary to even call a!localVariables() other than potentially if you want to add one in the future for easier understanding.

amans0009
January 5, 2024

I was going with a different approach with local variables and forgot to remove a!localVariables()  when I moved to rule inputs[emoticon:98f842990f454422aa6a04953955f9d9].

sriramk5349
January 14, 2024

Hi [mention:7cd3b09aef6f4206aa547fd04a01861e:e9ed411860ed4f2ba0265705b8793d05] ,

Below one might be helpful.

a!localVariables(
  local!list: { 67, 80, 100, 102, 150 },
  local!map: a!forEach(
    items: local!list,
    expression: a!map(number: fv!item)
  ),
  index(
    index(
      index(
        todatasubset(
          arrayToPage: local!map,
          pagingConfiguration: a!pagingInfo(
            startIndex: 1,
            batchSize: length(local!list),
            sort: a!sortInfo(field: "number", ascending: false)
          )
        ),
        "data",
        {}
      ),
      "number",
      {}
    ),
    1,
    {}
  )
)

mathieud0001
January 14, 2024

A couple of suggestions:

  • You can simply set the batchSize to 1 in the pagingConfiguration. With this you can reduce the number of times you use the index function. 
  • You can simply cast to an integer rather than getting the first element of the array, this removes another index.

a!localVariables(
  local!list: { 67, 80, 100, 102, 150 },
  local!map: a!forEach(
    items: local!list,
    expression: a!map(number: fv!item)
  ),
  local!dataSubset: todatasubset(
    arrayToPage: local!map,
    pagingConfiguration: a!pagingInfo(
      startIndex: 1,
      batchSize: 1,
      sort: a!sortInfo(field: "number", ascending: false)
    )
  ),
  cast(
    typeof(1),
    index(local!dataSubset.data, "number", null)
  )
)