To find the greatest number in the array without max function

conventional method to find the gratest number using for loop 

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    Hi  ,

    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,
        {}
      )
    )

Reply
  • 0
    Certified Senior Developer

    Hi  ,

    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,
        {}
      )
    )

Children
  • 0
    Certified Lead Developer
    in reply to Sri Ram Kaja

    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)
      )
    )