Skip to main content
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 - 1
test - 2
string - 1

A
Anonymous
June 3, 2026

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

  1. local!data holds the text "Hello, test string, test".
  2. 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.
  3. local!uniqData: union(local!data1, local!data1) removes the duplicates, giving { "Hello,", "test", "string," }, so each word is handled once.
  4. a!forEach(items: local!uniqData, expression: ...) loops over those three words, with fv!item holding the current one.
  5. wherecontains(fv!item, local!data1) returns the positions where the current word appears in the full word list, duplicates included.
  6. count(...) gives the number of those positions, which is how many times the word appears.
  7. 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

fv!item

Positions in

local!data1

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