Duplicate string in the string array

I have an array of strings i need the string which is duplicate . can u give me any simple functions to achieve this.

My array would be like this {"one","two","one","three"}

Thanks

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    So far the only solution that correctly addresses your requirement (returning the duplicated value(s), not returning a duplicates-removed array) was the answer submitted by  above.  I will provide my version here, slightly cleaner (imho) and removing one error in Vimal's version.

    with(
      
      /* example array - this can be replaced logically with whatever you need */
      local!items: {"one", "two", "one", "three"},
      
      local!duplicatesRemoved: union(local!items, local!items), /* in your environment, i suggest implementing a global rule to handle de-duplicating arrays, in case a better solution than "union" is found in the future */
      
      a!forEach(
        items: local!duplicatesRemoved,
        expression: if(
          apn_arraylength(wherecontains(fv!item, local!items)) > 1,
          fv!item,
          /* we need to return an empty set here (instead of 'null()') to avoid returning nulls in the resulting array */
          {}
        )
      )
    )
      

     Note that the "if" statement needs to return an empty set instead of "null", or else the resulting array will contain nulls along with the duplicate members, instead of just the duplicate members.

  • Thanks @Mike and your answers gave me what I want. But I was just looking for one function if we have directly so that reduces code.
  • 0
    Certified Lead Developer
    in reply to bhargavip
    There is no direct (out-of-box) function for this as far as I'm aware; in the past I've implemented this as a global Expression Rule which can receive any array and returns the duplicated members of that array, so that i can have consistency across my system (and not need to type out all that code over and over again).
Reply Children
No Data