Array#&() es un método de clase Array que realiza operaciones de intersección de conjuntos en las arrays. Y devuelve el común de las dos arrays.
Sintaxis: Array.&()
Parámetro: Arrays para realizar la operación de intersección.
Retorno: Elementos comunes de ambas arrays.
Ejemplo 1 :
# Ruby code for &() method # showing intersection operation # declaration of array a = [18, 22, 33, 4, 5, 6] # declaration of array b = [5, 4, 22, 1, 88, 9] # declaration of array c = [18, 22, 33, 40, 50, 6] # a intersecting b puts "intersection of a and b : #{a & b}\n\n" # a intersecting c puts "intersection of a and c : #{a & c}\n\n" # b intersecting c puts "intersection of b and c : #{b & c}\n\n"
Producción :
intersection of a and b : [22, 4, 5] intersection of a and c : [18, 22, 33, 6] intersection of b and c : [22]
Ejemplo #2:
# Ruby code for &() method # showing intersection operation # declaration of array a = ["abc", "xyz", "dog"] # declaration of array b = ["cow", "cat", "dog"] # declaration of array c = ["cat", "1", "dog"] # a intersecting b puts "intersection of a and b : #{a & b}\n\n" # a intersecting c puts "intersection of a and c : #{a & c}\n\n" # b intersecting c puts "intersection of b and c : #{b & c}\n\n"
Producción :
intersection of a and b : ["dog"] intersection of a and c : ["dog"] intersection of b and c : ["cat", "dog"]
Publicación traducida automáticamente
Artículo escrito por mayank5326 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA