Question
Given a string, write a program to count the frequency of each word and display the word along with its count.
Input:
"Hello, test string, test"Output:
Hello - 1test - 2string - 1
Given a string, write a program to count the frequency of each word and display the word along with its count.
Input:
"Hello, test string, test"
Output:
Hello - 1
test - 2
string - 1
A
Anonymous
June 3, 2026
91
Answer
Code:
a!localVariables(
local!data : "Hello, test string, test",
local!data1: split(local!data," "),
local!uniqData: union(local!data1,local!data1),
a!forEach(
items: local!uniqData,
expression: fv!item & "-" & count(wherecontains(fv!item,local!data1))
)
)
Output:
Hello - 1
test - 2
string - 1
Explanation:
This expression counts how many times each word appears in a sentence and returns each word once, followed by its count.
What each part does
local!dataholds the text"Hello, test string, test".local!data1: split(local!data, " ")splits the text at each space into a list of words:{ "Hello,", "test", "string,", "test" }. The commas stay attached because the split only looks at spaces.local!uniqData: union(local!data1, local!data1)removes the duplicates, giving{ "Hello,", "test", "string," }, so each word is handled once.a!forEach(items: local!uniqData, expression: ...)loops over those three words, withfv!itemholding the current one.wherecontains(fv!item, local!data1)returns the positions where the current word appears in the full word list, duplicates included.count(...)gives the number of those positions, which is how many times the word appears.fv!item & "-" & count(...)joins the word, a hyphen, and the count into one text value. Appian converts the number to text automatically.
Step by step
| Positions in
| Count | Returned |
|---|---|---|---|
Hello, | { 1 } | 1 | “Hello,-1” |
test | { 2, 4 } | 2 | “test-2” |
string, | { 3 } | 1 | “string,-1” |
Result
{ "Hello,-1", "test-2", "string,-1" }
For more coding challenges visit : https://appianverse.com/coding
Try it yourself
Change the inputs or the expression and run it to see what it returns.
ri!data=
Independent evaluator: it approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms
JuniorExpressions#coding