Manage Learn to apply best practices and optimize your operations.

Counting unique elements in a text list

This tip's formula will count the number of elements that are the same in a text list.

The following formula will count the number of elements that are the same in a text list.

```List1 :=  @Explode ( "Red,Blue,Red,
Green,Red,Blue"; "," ) ;
List2 := List1 + "@" ;
List3 := @Unique ( List1 ) ;
List4 := @ReplaceSubstring
( List3; List1; List2 ) ;
List5 := @ReplaceSubstring
( List4; List3; "" ) ;
@Length ( List5 )
```

In this example it returns 3; 2; 1. That is, there are 3 occurrences of red, 2 blue and 1 green.

How it works

List1 can be any text list field or variable.

List2 is all the elements of List1 with @ appended to the end. You can use other characters. So, in this example, List2 looks like:

``` Red@; Blue@; Red@; Green@; Red@; Blue@
```

List3 is a text list of the unique values within List1:

``` Red; Blue; Green
```

List4 replaces the values in List3 with those in List2, which results in an @ sign for each occurrence:

``` Red@@@; Blue@@; Green@
```

List5 removes the List3 values from List4 and returns:

``` @@@; @@; @
```

The last command now returns the length of each item in the list:

``` 3; 2; 1
```

With a bit more list manipulation you can return:

Red 3
Blue 2
Green 1

```List1 :=  @Explode ( "Red,Blue,Red,
Green,Red,Blue"; "," ) ;
List2 := List1 + "@" ;
List3 := @Unique ( List1 ) ;
List4 := @ReplaceSubstring ( List3;
List1; List2 ) ;
List5 := @ReplaceSubstring ( List4;
List3; "" ) ;
@Length ( List5 )
```

Do you have comments on this tip? Let us know.

SearchWindowsServer

• Exchange Server bugs continue to bite on April Patch Tuesday

Microsoft resolves 110 vulnerabilities, including a zero-day and four public disclosures, but the company says admins should ...

• Azure MFA NPS extension boosts authentication capabilities

With help from the Network Policy Server extension, organizations can expand their on-premises authentication features by tapping...

• Azure File Sync service puts pep in hybrid storage setups

Microsoft's Azure Files offering alleviates traditional support efforts and hardware costs. Using this additional service can ...

Search400

• Favorite iSeries cheat sheets

Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/Search400.com community. These cheat ...

• HTML cheat sheet

This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

• Carol Woodbury: Security

Carol Woodbury

SearchDataCenter

• Programmable processor technology for next-gen data centers

The right processing technology can benefit your data center. Learn about advancements in CPU technologies, recent vendor ...

• Data processing units accelerate infrastructure performance

DPUs often run on networking packets to move information in the data center, instead of supporting processing workflows. Get an ...

• New Intel Ice Lake processors boost performance, security

Intel launches third-generation Xeon Scalable processors that bolster security, accelerate common data center workloads by 46% on...

SearchContentManagement

• OpenText releases Cloud Editions content services updates

OpenText CE 21.2 includes federated document compliance that extends to Microsoft Office 365, along with a revamped content ...