Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Comparison between arrays

ujjwal Gautam
Tera Contributor

hi all,

We have two array

var arrayA = { "a","b","c"};

var arrayB ={"c","d","e"};

we need to items, which is available  in arrayA but  not available arrayB. Expected o/p is :"a","b";

please  suggest for approach.

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Try something as below.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.diff(a1, a2)); //will print a,b

gs.print(arrayUtil.diff(a2, a1));//will print b,c

Refer link

View solution in original post

7 REPLIES 7

Jaspal Singh
Mega Patron
Mega Patron

Try something as below.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.diff(a1, a2)); //will print a,b

gs.print(arrayUtil.diff(a2, a1));//will print b,c

Refer link

Last line had a typo it will print d,e instead of b,c

gs.print(arrayUtil.diff(a2, a1));//will print d,e

Thanks Jaspal

 

chetan17421
Tera Guru

Hi,

You can use the 'ArrayUtil' Script include function which is available OOB. Please see the use case below,

var a = [1,2,3,4];

var b = [2,3];

var c = new ArrayUtil().diff(a,b);

gs.info(c); // This will print > 1,4