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.

Reply
  • +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.

Children