DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Step Into Serverless Computing
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective

Trending

  • Step Into Serverless Computing
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  1. DZone
  2. Data Engineering
  3. Data
  4. Identifying Duplicate Values in an Array using Mule 4 DataWeave 2.0

Identifying Duplicate Values in an Array using Mule 4 DataWeave 2.0

This brief tutorial shows how to create a function with DataWeave that helps identify duplication in an array.

Sravan Lingam user avatar by
Sravan Lingam
CORE ·
Aug. 17, 20 · Tutorial
Like (5)
Save
Tweet
Share
16.28K Views

Join the DZone community and get the full member experience.

Join For Free

Hi Guys,

We might have seen how to remove duplicates using the distinctBy function of DW 2.0.

But what if we have a scenario where we need to know what are the values that are repeated more than once?

In that case, I have written a small function to identify the duplicate values that are present in an array.

Just to explain about the function I have written below:

  • distinctVal is a variable which returns distinct Values by removing duplicates
  • duplicates is a variable which iterates distinctVal data which we got in the above step over original data and checks if the value is repeated more than once.

Let's consider an array which has values  [  "a" , "b" ,"a" ,"c" ,"d", "e" ,"c" ,"f" ]

In the above array, we see that values "a" and "c" are repeated more than once and we have to find that using DataWeave code. Let's do it!

DataWeave code:

JSON
 




xxxxxxxxxx
1
25


 
1
%dw 2.0
2

          
3
output application/json  
4

          
5
var record =     [    "a" , "b" ,"a" ,"c" ,"d", "e" ,"c" ,"f" ]
6

          
7
 var distinctVal = record distinctBy $
8

          
9
 var duplicates = (distinctVal map(key,value) -> {
10

          
11
    count : if(sizeOf((record map $ == key) filter $ ) > 1) key else null
12

          
13
}) filter $.count !=null 
14

          
15
---
16

          
17
{
18

          
19
    distincValues : distinctVal,
20

          
21
    
22

          
23
    duplicatesAre : duplicates.count
24

          
25
}


Output :

Java
 




x
27


 
1
{
2

          
3
  "distincValues": [
4

          
5
    "a",
6

          
7
    "b",
8

          
9
    "c",
10

          
11
    "d",
12

          
13
    "e",
14

          
15
    "f"
16

          
17
  ],
18

          
19
  "duplicatesAre": [
20

          
21
    "a",
22

          
23
    "c"
24

          
25
  ]
26

          
27
}


So here :

"distinctValues" are the final array of values by removing the values which are repeated

"duplicatesAre" will display the values that are repeated more than once.

In this way, you can find the duplicate values in an array using DataWeave 2.0


Happy Learning!

Data structure

Opinions expressed by DZone contributors are their own.

Trending

  • Step Into Serverless Computing
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: