Compare two Arrays

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dahaarl
:warning: Old Version Published before Godot 3 was released.

Hello Godot users !

I’m quite a noob in (gd)script and especially in Arrays so I wonder how I could compare data between two arrays.

My goal is to find all sports in common between all this people and save them in the “Sports” array. I know that it’s possible in other languages but I can’t figure it out in GdScript.

var Agnes = [ 
			"Basketball",
			"Football",
			"Handball",
			"Tennis",
			"Rugby"
			]

var John = [ 
			"Basketball",
			"Football",
			"Handball",
			"Tennis"
			]
			
var Paul = [ 
			"Basketball",
			"Handball",
			"Tennis",
			"Rugby"
			]
			
var Cindy = [ 
			"Handball",
			"Tennis",
			"Rugby"
			]
			
var Sports = []

Thanks !

:bust_in_silhouette: Reply From: anakimluke

You can do

for sport in Cindy:
    if (sport in Agnes) and (sport in John) and (sport in Paul):
        Sports+=[sport]
print("Sports that all of them do: ", Sports)

I chose to do the for with Cindy because it’s the smallest list.

Waow thank you ! Your solution is way more easier than mine and works better too !
Thank you again !

Dahaarl | 2017-01-29 12:38