`{"id":"dabe49fa-0176-47b5-9bf8-f77b598e780b","name":"Get Common Elements - 2","description":"<p>1. You are given a number n1, representing the size of array a1. 2. You are given n1 numbers, representing elements of array a1. 3. You are given a number n2, representing the size of array a2. 4. You are given n2 numbers, representing elements of array a2. 5. You are required to find the intersection of a1 and a2. To get an idea check the example below: if a1 -&gt; 1 1 2 2 2 3 5 and a2 -&gt; 1 1 1 2 2 4 5 intersection is -&gt; 1 1 2 2 5 Note -&gt; Don't assume the arrays to be sorted. Check out the question video.</p>","inputFormat":"<p>A number n1 n1 number of elements line separated A number n2 n2 number of elements line separated</p>","outputFormat":"<p>All relevant elements of intersection in separate lines The elements of intersection should be printed in order of their occurence in a2.</p>","constraints":"<p>1 &lt;= n1, n2 &lt;= 100 0 &lt;= a1[i], a2[i] &lt; 10 Time complexity should be O(n)</p>","sampleCode":{"cpp":{"code":"def main():\r\n n1 = int(input())\r\n arr1=[]\r\n for i in range(n1):\r\n arr1.append(int(input()))\r\n n2 = int(input())\r\n arr2=[]\r\n for i in range(n2):\r\n arr2.append(int(input()))\r\n\r\n #write your code here\r\n \r\n \r\nif __name__ == '__main__':\r\n main()"},"java":{"code":"import java.io.*;\r\n\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n\t //Write your code here\r\n \r\n }\r\n\r\n}"},"python":{"code":"def main():\n n1 = int(input())\n arr1=[]\n for i in range(n1):\n arr1.append(int(input()))\n n2 = int(input())\n arr2=[]\n for i in range(n2):\n arr2.append(int(input()))\n\n #write your code here\n \n \nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1\r\n1\r\n2\r\n2\r\n2\r\n3\r\n5\r\n7\r\n1\r\n1\r\n1\r\n2\r\n2\r\n4\r\n5","sampleOutput":"1\r\n1\r\n2\r\n2\r\n5","questionVideo":"https://www.youtube.com/embed/hBX47E5WDIA","hints":[],"associated":[{"id":"8d31c6ee-980f-467f-a077-a449cc672d24","name":"What is the time complexity to get common elements from hashmap ? (Get common elements-2)","slug":"what-is-the-time-complexity-to-get-common-elements-from-hashmap-get-common-elements-2","type":4},{"id":"b05ff4d8-1829-4e43-92b5-03f06dc2d119","name":"What is the space complexity to get common elements from hashmap ? (Get common elements-2)","slug":"what-is-the-space-complexity-to-get-common-elements-from-hashmap-get-common-elements-2","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"69feaaae-b3ad-4b50-b8c2-b41216ff2e6a","name":"Get Common Elements - 2","slug":"get-common-elements-2","type":1}],"next":{"id":"61ba3f11-6c62-476c-9f17-5ad8b71d6804","name":"Get Common Elements-2","type":3,"slug":"get-common-elements-2"},"prev":{"id":"f0638035-136e-4500-a53b-aef089fa6bef","name":"Get Common Elements-1","type":3,"slug":"get-common-elements-1"}}}`

# Get Common Elements - 2

<p>1. You are given a number n1, representing the size of array a1. 2. You are given n1 numbers, representing elements of array a1. 3. You are given a number n2, representing the size of array a2. 4. You are given n2 numbers, representing elements of array a2. 5. You are required to find the intersection of a1 and a2. To get an idea check the example below: if a1 -&gt; 1 1 2 2 2 3 5 and a2 -&gt; 1 1 1 2 2 4 5 intersection is -&gt; 1 1 2 2 5 Note -&gt; Don't assume the arrays to be sorted. Check out the question video.</p>

`{"id":"dabe49fa-0176-47b5-9bf8-f77b598e780b","name":"Get Common Elements - 2","description":"<p>1. You are given a number n1, representing the size of array a1. 2. You are given n1 numbers, representing elements of array a1. 3. You are given a number n2, representing the size of array a2. 4. You are given n2 numbers, representing elements of array a2. 5. You are required to find the intersection of a1 and a2. To get an idea check the example below: if a1 -&gt; 1 1 2 2 2 3 5 and a2 -&gt; 1 1 1 2 2 4 5 intersection is -&gt; 1 1 2 2 5 Note -&gt; Don't assume the arrays to be sorted. Check out the question video.</p>","inputFormat":"<p>A number n1 n1 number of elements line separated A number n2 n2 number of elements line separated</p>","outputFormat":"<p>All relevant elements of intersection in separate lines The elements of intersection should be printed in order of their occurence in a2.</p>","constraints":"<p>1 &lt;= n1, n2 &lt;= 100 0 &lt;= a1[i], a2[i] &lt; 10 Time complexity should be O(n)</p>","sampleCode":{"cpp":{"code":"def main():\r\n n1 = int(input())\r\n arr1=[]\r\n for i in range(n1):\r\n arr1.append(int(input()))\r\n n2 = int(input())\r\n arr2=[]\r\n for i in range(n2):\r\n arr2.append(int(input()))\r\n\r\n #write your code here\r\n \r\n \r\nif __name__ == '__main__':\r\n main()"},"java":{"code":"import java.io.*;\r\n\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n\t //Write your code here\r\n \r\n }\r\n\r\n}"},"python":{"code":"def main():\n n1 = int(input())\n arr1=[]\n for i in range(n1):\n arr1.append(int(input()))\n n2 = int(input())\n arr2=[]\n for i in range(n2):\n arr2.append(int(input()))\n\n #write your code here\n \n \nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1\r\n1\r\n2\r\n2\r\n2\r\n3\r\n5\r\n7\r\n1\r\n1\r\n1\r\n2\r\n2\r\n4\r\n5","sampleOutput":"1\r\n1\r\n2\r\n2\r\n5","questionVideo":"https://www.youtube.com/embed/hBX47E5WDIA","hints":[],"associated":[{"id":"8d31c6ee-980f-467f-a077-a449cc672d24","name":"What is the time complexity to get common elements from hashmap ? (Get common elements-2)","slug":"what-is-the-time-complexity-to-get-common-elements-from-hashmap-get-common-elements-2","type":4},{"id":"b05ff4d8-1829-4e43-92b5-03f06dc2d119","name":"What is the space complexity to get common elements from hashmap ? (Get common elements-2)","slug":"what-is-the-space-complexity-to-get-common-elements-from-hashmap-get-common-elements-2","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"69feaaae-b3ad-4b50-b8c2-b41216ff2e6a","name":"Get Common Elements - 2","slug":"get-common-elements-2","type":1}],"next":{"id":"61ba3f11-6c62-476c-9f17-5ad8b71d6804","name":"Get Common Elements-2","type":3,"slug":"get-common-elements-2"},"prev":{"id":"f0638035-136e-4500-a53b-aef089fa6bef","name":"Get Common Elements-1","type":3,"slug":"get-common-elements-1"}}}` Editor

# Get Common Elements - 2

easy

1. You are given a number n1, representing the size of array a1. 2. You are given n1 numbers, representing elements of array a1. 3. You are given a number n2, representing the size of array a2. 4. You are given n2 numbers, representing elements of array a2. 5. You are required to find the intersection of a1 and a2. To get an idea check the example below: if a1 -> 1 1 2 2 2 3 5 and a2 -> 1 1 1 2 2 4 5 intersection is -> 1 1 2 2 5 Note -> Don't assume the arrays to be sorted. Check out the question video.

## Constraints

1 <= n1, n2 <= 100 0 <= a1[i], a2[i] < 10 Time complexity should be O(n)

## Format

### Input

A number n1 n1 number of elements line separated A number n2 n2 number of elements line separated

### Output

All relevant elements of intersection in separate lines The elements of intersection should be printed in order of their occurence in a2.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 1 1 2 2 2 3 5 7 1 1 1 2 2 4 5```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}1 1 2 2 5```

Question Video

Discussions

Show Discussion

Related Resources 