Imagine you’re a programmer and you have a list of numbers. Just like sorting balls with numbers into groups, this script helps you sort the numbers.

Imagine you’re a programmer and you have a list of numbers. Just like sorting balls with numbers into groups, this script helps you sort the numbers. It puts numbers that show up more than once in one group, and numbers that are unique in another. It’s a way to organize and understand your data quickly, just like you would sort balls in a bag, but here, it’s all done with a dataweave script.
Starting Off: What’s the Plan?
%dw 2.0 output application/jsonimport * from dw::core::ArraysThis part is like saying, “Hey computer, we’re going to use a special way of sorting numbers (DataWeave 2.0), and when we’re done, show us the list in a way that’s easy to read (JSON format). Also, let’s use all the great sorting tools available (Arrays).”
The Numbers: What Are We Sorting?
var inputData = [ 3, 1, 5, 4, 2, 1, 5, 6, 7, 1 ]{ "non-duplicate": [ { "value": "3", "count": 1 }, { "value": "4", "count": 1 }, { "value": "2", "count": 1 }, { "value": "6", "count": 1 }, { "value": "7", "count": 1 } ], "duplicate": [ { "value": "1", "count": 3 }, { "value": "5", "count": 2 } ]}Here are our numbers, just like the balls in the bag. We have a few duplicates, like 3 and 4, and some single ones like 1 and 6.
The Magic: Sorting the Numbers
---inputData groupBy ($) pluck( if(sizeOf($)>1){ "value" : $$, "count" : sizeOf($) } else{"value" : $$, "count" : sizeOf($) } ) groupBy ( $.count > 1 ) mapObject( if($$ ~= true)("duplicate" : $ ) else("non-duplicate" : $ ) )The script sorts numbers into two types. Numbers that are repeated go into a group called “Duplicates”. Numbers without repeats go into another group called “Non-Duplicates”. It’s like having two containers where you separate numbers into either “Duplicates” or “Non-Duplicates” depending on if they are repeated or not.
The Grand Reveal: Our Organized Script
%dw 2.0import * from dw::core::Arraysvar inputData = [3, 1, 5, 4, 2, 1, 5, 6, 7, 1]output application/json---inputData groupBy ($) pluck (if (sizeOf($) > 1) {"value": $$, "count": sizeOf($) }else {"value": $$, "count": sizeOf($)}) groupBy ($.count > 1) mapObject (if ($$ ~= true) ("duplicate": $)else ("non-duplicate": $))The Result: A Neatly Sorted List
Finally, it will gives us a clear list. It points out the numbers that are duplicates, counts how often they show up, and identifies the ones that are one-of-a-kind. This makes it simple to understand the situation with our numbers.
{ "duplicate": [ {"value": "3", "count": 2 },{ "value": "4", "count": 2 },{ "value": "5", "count": 2 } ],
"non-duplicate": [
{ "value": "1", "count": 1 },{ "value": "2", "count": 1 },{ "value": "6", "count": 1 } ]
}