cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Garima
Frequent Visitor

Compare two array objects

I am looking for a way to compare two array objects. Both the objects have email address and I want only those email address that are unique to the second one.

 

For example -

Array1 - [ test@abc.com, test1@abc.com, new@xyz.com]

Array2 - [ test@abc.com, test1@abc.com, test2@xyz.com]

 

What I want is -> test2@xyz.com. 

 

Basically opposite of intersection operation.

If not with array, is it possible to get this using string? 

11 REPLIES 11
v-xida-msft
Community Support
Community Support

Hi @Garima,

 

Do you want to compare two array objects and find the unique elements within the second array?

 

I have made a test on my side and please take a try with the following workaround:

  • Add a proper trigger, here I use Flow Button trigger.
  • Add a Variables-> "Initialize variable" action, Name set to Array1, Type set to Array and Value set to following formula:

 

["test@abc.com","test1@abc.com"," new@xyz.com"]

 

  • Add a Variables-> "Initialize variable 2" action, Name set to Array2, Type set to Array and Value set to following formula:
["test@abc.com","test1@abc.com","test2@xyz.com"]
  • Add a Variables-> "Initialize variable 3" action, Name set to UniqueEmails, Type set to Array and Value set to empty.
  • Add a "Apply to each" action, input parameter set to Array2 variable. Within "Apply to each" action, add a Condition, click "Edit in advanced mode", type the following formula:
@contains(variables('Array1'), item())

Within "If/no" branch of Condition, add a "Append to array variable" action, Name set to UniqueEmails variable, Value set to following formula:

item()
  • Under "Apply to each" action, add a "Compose" action, Inputs field set to UniqueEmails variable.

Image reference:1.JPG

 

2.JPG

The flow works successfully as below:3.JPG

 

More details about using expression in flow actions, please check the following article:

Use expression in flow actions

 

If you want to compare two strings (compare characters within two strings), I afraid that there is no direct way to achieve your needs in Microsoft Flow currently.

 

Best regards,

Kris

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

This is awesome! thank you. 

 

Question: what if i wanted to find unique records based on a difference in a single field? 

 

for example

 

if Array1 has a "Title" Field and an "Email" field

and Array 2 has a "Title" field and an "Email" field

 

I want to populate ArrayUniqueEmails only where the title doesn't exist?

 

Can this be done? 

I would like to know this too

Hi, @v-xida-msft , this looks great ... however there's no option to edit the condition in advanced mode. How do we get there?

 

EDIT -- 

Figured it out, I'm an idiot, but others may be as dumb as me.

noAdvance.gif

JE-ax
Advocate I
Advocate I

@Garima 

Use Filter Array

 
 

image.png

 

Result is: [ "3" ]

DudelerDad
Frequent Visitor

Eliot just so you know  there are others as I have been struggling with this but your simple solution should work for me.

Thanks.

Powerplatform01
Post Patron
Post Patron

@Garima 

You can watch this video to resolve your issue.

Also request you to please subscribe this PowerAutomate channel

https://youtu.be/OIIjOy-p-UE

nitinkhubani
New Member

I actually found a better way for comparing two arrays without Apply to Each loop

 

equals(length(intersection(variables('Array 1'),variables('Array 2'))),length(variables('Array 1')))

Nicely done nitinkhubani

But then you only know if the lengths of the arrays are different. But you still need the values from Array2.

True MVP! This is the fastest way to compare two arrays and return the difference from one of them.

You can repeat the function and swap out the sources in the From and Contains parameters to return the difference for both. 

I must have seen a dozen different solutions from Microsoft support staff that could not provide this elegant formula. THANK YOU!

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (1,736)