Common elements of two lists
Write a function that receives two lists (array) and the common elements in the two lists in the new list
Write a function that receives two lists (array) and the common elements in the two lists in the new list
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
list1 = ["ali" , 1 , 2 , 3 ,"Reza"] list2 = ["Mamali" ,3 , 5 , "Sara" , "Reza"] list3 = [] for word in list1: if word in list2: list3.append(word) print(list3)
l1 = input("enter element l1: ").split(" ")
l2 = input("enter element l2: ").split(" ")
s1 = set(map(int, l1))
s2 = set(map(int, l2))
print(list(s1 & s2))
let arr1=[10,11,12,15,17] let arr1=[10,21,18,45,17] let finalArr=[] arr1.forEach(function(num){ arr2.forEach(function(num2){ if(num===num2){ finalArr.push(num) } }) }) console.log(finalArr)
lst1=[5,6,9,10,66,57,82] lst2=[66,5,667,84,5,57,33] lst3=[] for i in range(len(lst1)): for j in range(len(lst2)): if lst1[i]==lst2[j]: lst3.append(lst1[i]) print(lst1) print(lst2) print(lst3)
def Moshtarak(n , m) :
s = []def Moshtarak(n , m) :
s = []
for i in n :
if i in m :
s.append(i)
return s
n = eval(input())
m = eval(input())
print(Moshtarak(n , m))
for i in n :
if i in m :
s.append(i)
return s
n = eval(input())
m = eval(input())
print(Moshtarak(n , m))
let listArr1=[1,2,3,4,5]; let listArr2 = [2,4,6,8,10]; function CommonElement(listArr1,listArr2) { let newListArr =[]; listArr1.forEach(x => { let elemantOne =x; listArr2.forEach(y =>{ let elemantTwo =y; if(elemantOne == elemantTwo){ newListArr.push(elemantOne); } }); }); console.log(newListArr); } CommonElement(listArr1,listArr2);
package Tax.App; import java.util.ArrayList; public class Anasor_Moshtarak { public static void main(String[] args) { int[] list1 = {7, 6, 17, 11, 19}; int[] list2 = {7, 8, 9, 10, 11,}; ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < list1.length; i++) { for (int j = 0; j < list2.length; j++) { if (list1[i] == list2[j]) { list.add(list1[i]); } } } System.out.print(list); } }
Submitting answers is currently unavailable.
You must be logged in to access this section.
Login/Sign up If you don’t understand the exercise or can’t solve it for any reason, that’s completely
normal—don’t worry 😊
Try checking out easier exercises and reviewing different answers
submitted by others. Gradually, you can move on to more challenging exercises. Also, your answer
might be correct even if it’s different from others.