Dataweave Series for Practice & Interview Part-7

Shubham Chaurasia
2 min readJan 8, 2024

--

Practice Dataweave here :- Dataweave Playground

Important Note:- First Copy Input, Output, DW Code & Use Json Online Validator, to format the JSON, since medium changes the JSON.

  1. Given a list of numbers, write a function to find the first number that appears more than once

Input:-

[1,2,3,1,2]

Output:-

1

Dataweave Code:-

%dw 2.0
output application/json
var seen = []
fun findFirstDuplicate(numbers,seen=[]) =
numbers map ((item, index) ->
if (seen contains (item) ~= true)
log(findFirstDuplicate(numbers[1 to -1],(seen + item)))
else log(item)
)
---
findFirstDuplicate(payload,seen)

2. Given a list of words, write a function to find the longest palindromic substring

Input:-

Output:-

Dataweave Code:-

3. Given a Number, Find its Factorial

Input:-

6

Output:-

720

Dataweave Code:-

%dw 2.0
output application/json
fun Factorial(n:Number, prod:Number=1) =
(if (n==1 or n==0) prod
else Factorial(n-1,(prod*n)))
---
Factorial(payload)

4. Sum of Digits of a Number till its a single digit. (E.g:- 789 -> 7+8+9 = 24 | 2+4 = 6)

Input:-

789

Output:-

6

Dataweave Code:-

%dw 2.0
import lines from dw::core::Strings
output application/json
fun sumOfDigits(num, result=0) =
do {
var new = (num reduce $+$$) as String
---
if (sizeOf(new) > 1) sumOfDigits(new, result+new)
else new
}
---
sumOfDigits(sum(lines(payload))) as Number

5.

Input 1:-

[
{
"fName": "Max",
"lName": "Mule"
},
{
"fName": "John",
"lName": "Paul"
},
{
"fName": "balu",
"lName": "Paul"
},
{
"fName": "Max",
"lName": "Paul"
},
{
"fName": "piki",
"lName": "Paul"
}
]

Input 2:-

var a = ["Max", "balu"]

Output:-

[
{
"fName": "Max",
"lName": "Mule"
},
{
"fName": "Max",
"lName": "Paul"
}
]

Dataweave Code:-

%dw 2.0
output application/json
var a = ["Max", "balu"]
---
payload map ((item, index) ->
{
"fname":(a filter($==item.fName))[0],
"lname":item.lName
}
)filter ((item, index) -> !(item.fname==null)) distinctBy $.fname

6.

Input:-

[1,3,2,2,5,5,5,4]

Output:-

[
{
"duplicates": "Number is: 2 size is 2"
},
{
"duplicates": "Number is: 5 size is 3"
}
]

Dataweave Code:-

%dw 2.0
output application/json
---
(((payload groupBy $) pluck $) map (value) -> {
(duplicates: "Number is: " ++ value[0] ++ " size is " ++ sizeOf(value)) if sizeOf(value) > 1
}) filter ($ != {})

📢 Check the other Articles in the Dataweave Series:- Dataweave Series for Practice & Interview Part 1–10

Thank You for sticking with the article until the end. If you found this helpful, please leave a Clap & Follow for more such amazing articles in the future.

Also Checkout More Amazing Articles:- Click Here

About Me:-

You can read more about me here. Follow me for Amazing Blogs & Articles.

Feel Free to Drop your queries on my below Communication Channels

Gmail:-myid535@gmail.com
LinkedIn:- https://www.linkedin.com/in/shubhamchaurasia1/

--

--

Shubham Chaurasia

MuleSoft Ambassador | Mule Certified Architect & Developer | 11x Salesforce| 4x AWS | 2x GCP | 2x Solace | 2xAzure | https://linkedin.com/in/shubhamchaurasia1/