Is there a way to compare two arrays and return a new array of the common elements which have the same index?

dedalus

For example

a = [2,3,1,1]
b = [2,7,4,2]

--> c = [2]

My solution was:

c = b.select do
    |em| b.index(em) == a.index(em)
    end

But if I apply it to the given example it returns

c = [2,2]
Sergio Tulentsev

Since you want to compare arrays element-wise, zip would be an excellent choice here.

a.zip(b) # => [[2, 2], [3, 7], [1, 4], [1, 2]]

a.zip(b).select {|a1, b1| a1 == b1}.map(&:first) # [2]

# or in ruby 2.7+
a.zip(b).filter_map {|a1, b1| a1 == b1 && a1} # [2]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare arrays, find same elements and return index

Is there a way to check if two arrays have the same elements?

Make a new array with common elements in two arrays

compare two arrays for common Items and return another array with the common items

Compare two json arrays and return common elements java

How to compare two arrays to see how many same elements they have?

Compare two array and return an array to show which elements are equal or not

Most efficient way to return common elements from two string arrays

Compare two excel files in Pandas and return the rows which have the same values in TWO columns

Scala: Compare elements at same position in two arrays

How to compare two arrays and then return the index of the difference?

Compare two arrays of hashes and return new object

Compare two list and get the index of same elements

Java - Compare Two List of String if they have common value and return the how many item is the same

Return the index of the common elements of two lists

Compare two lists to return a list with all elements as 0 except the ones which matched while keeping index?

How to compare two arrays and then return the sum of all elements of the array before the changed element of the array

How to return the number of same elements on same index position when comparing two arrays

JavaScript compare two arrays and push items to the first array that have the same property value in the second array

Checks if two arrays have elements in the same order

How to deep compare two javascript objects and return all the differences including new added arrays and with the same original format?

Compare two arrays and append to new array

Process elements at the same index of two arrays

How to eliminate the rows from an array which have common elements at different index position in python?

PHP - Looking for a way to compare a value in two dimensional array, then combine arrays with same value to form a three dimensional array

How to add two elements of arrays with each others and have a new array with a new shape?

Javascript: How to compare two arrays, and return the name of the array with highest value in a certain index

check two arrays for matching elements given the same index and return number of matches

C++ function to return same-index elements of two char arrays

TOP Ranking

HotTag

Archive