Skip to main content
August 8, 2023
Question

Get duplicates from array

  • August 8, 2023
  • 4 replies
  • 0 views

How to get the duplicates from an array:

i/p:{1,2,2,3,3}
o/p:{2,3}

    4 replies

    August 8, 2023

    Hi, Please try to use this

    a!localVariables(
      local!a:{1,2,2,3,3},
      a!flatten(a!forEach(
        items: union(local!a,local!a),
        expression: if(
          length(wherecontains(fv!item,local!a))>1,
          fv!item,
          {}
        )
      ))
    )

    jamesm4933
    August 8, 2023

    This seems to work, too.

    a!localVariables(
      local!input: { 1, 2, 2, 3, 3 },
      local!output: index(local!input, a!forEach(union(local!input, local!input), index(wherecontains(fv!item, local!input), 2, {}))),
      local!output
    )

    jamesm4933
    August 8, 2023

    Don't use, but it may be of interest:

    a!localVariables(
      local!input: { 1, 2, 2, 3, 3 },
      local!output: index(local!input, where(a!forEach(local!input, fv!index>wherecontains(fv!item, local!input)[1]))),
      local!output
    )

    csteward
    August 8, 2023

    Note this last example will need a union() on the local!output as it duplicates values with more than 2 occurrences.