How do you remove duplicate values in an array in Appian ?
How do you remove duplicate values in an array in Appian ?
Answer
Will use union() to remove the duplicate elements
Code:
Output:

Why union works here: union() merges lists, and the merged list holds each distinct value only once. That applies to duplicates inside a single list as well as across lists, so the output has no repeats even though both arguments are the same list.
The function expects at least two lists, so the list is passed twice. The second copy adds no new values, which leaves the first list with its duplicates removed.
Identify the duplicate elements in array and print it : Also they might may be a follow up question to write a code in Appain to print duplicate elements only from the given array. Below this code to print duplicate elements from the array
Code:
Output:
{"Naruto", "Itachi"}
Explanation:
This expression finds the names that appear more than once in a list and returns each of them once. Simply returns duplicates
What each part does
local!usersis a list of six names. “Naruto” and “Itachi” each appear twice.local!uniqueUsers: union(local!users, local!users)removes the duplicates, giving{ "Naruto", "Sasuke", "Itachi", "Madhara" }, so each name is checked once instead of once per occurrence.a!forEach(local!uniqueUsers, ...)loops over those four names, withfv!itemholding the current one.wherecontains(fv!item, local!users)returns the positions where the current name appears in the original list, duplicates included. For “Naruto” that is{ 1, 4 }.count(...) > 1is true when the name appears at more than one position, so it is a duplicate.if(..., fv!item, "")returns the name for a duplicate and an empty string otherwise.reject(fn!isnull, ...)removes the empty strings, leaving only the duplicated names.
Step by step
| Positions in
| Count | Returned |
|---|---|---|---|
Naruto | { 1, 4 } | 2 | “Naruto” |
Sasuke | { 2 } | 1 | " " |
Itachi | { 3, 6 } | 2 | “Itachi” |
Madhara | { 5 } | 1 | " " |
After the loop, the list is { "Naruto", "", "Itachi", "" }. Reject(fn!isnull,..) removes the empty values form the array.
Result
Try it yourself
Change the inputs or the expression and run it to see what it returns.
Independent evaluator: it approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms