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

  • Hi,
    Could you please try the union().

    Thanks,
    Sudha.P
  • Union() -Remove the duplicates from an array by comparing it with an empty array of the same type:

    union({1, 2, 3, 4, 1, 2}, tointeger({})) returns and array with 1, 2, 3, 4

    Values are matched with case sensitivity
  • 0
    Certified Lead Developer
    Hi you can use following code to achieve your requirement

    union({"a","b","a","c"},{"a","b","c","a"})

    Assume {"a","b","a","c"} is the array which is available in a variable
  • +1
    Certified Lead Developer

    Hi ,

    Assuming if the array is  {"one","two","one","three"} and if you are looking for the duplicate values in this array which is {"one"}.

    You can use the below code for this

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    reject(
    fn!isnull,
    a!forEach(
    items: union({"one","two","one","three"},{"one","two","one","three"}),
    expression: if(
    count(
    wherecontains( fv!item, {"one","two","one","three"})) > 1,
    fv!item,
    null
    )
    )
    )
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Hi

    Use intersection function

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    =load(
    local!StringInput: {"one","two","one","three"},
    intersection(local!StringInput,local!StringInput)
    )
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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 */
    {}
    )
    )
    )
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      

     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).
  • 0
    A Score Level 1
    in reply to bhargavip
    As has said, there's no one function for this. You might find it useful to refer to the searchable list of functions here: docs.appian.com/.../Appian_Functions.html