`{"id":"e52a3f6d-53b2-4b1b-9b66-1926a2ee03f7","name":"Get Common Elements - 1","description":"1. You are given a number n1, representing the size of array a1.\r\n2. You are given n1 numbers, representing elements of array a1.\r\n3. You are given a number n2, representing the size of array a2.\r\n4. You are given n2 numbers, representing elements of array a2.\r\n5. You are required to print all elements of a2 which are also present in a1 (in order of their occurence in a2). Make sure to not print duplicates (a2 may have same value present many times).","inputFormat":"A number n1\r\nn1 number of elements line separated\r\nA number n2\r\nn2 number of elements line separated","outputFormat":"All relevant elements of a2 in separate lines (no duplicacy)","constraints":"1 &lt;= n1, n2 &lt;= 100\r\n0 &lt;= a1[i], a2[i] &lt; 10\r\nTime complexity should be O(n)","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <unordered_map>\nusing namespace std;\n\nint main() {\n int n1,n2;\n cin >> n1;\n int arr1[n1];\n \n for (int i = 0; i < n1; i++) {\n cin>>arr1[i];\n mp[arr1[i]]++;\n }\n cin >> n2;\n int arr2[n2];\n for (int i = 0; i < n2; i++) {\n cin >> arr2[i];\n \n // write your code here\n \n \n }\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"n1 = int(input())\narr1=[]\nfor x in range(n1):\n arr1.append(int(input()))\n\nn2 = int(input())\narr2=[]\nfor x in range(n2):\n arr2.append(int(input()))\n\n \n#write your code here"}},"points":10,"difficulty":"easy","sampleInput":"9\r\n5\r\n5\r\n9\r\n8\r\n5\r\n5\r\n8\r\n0\r\n3\r\n18\r\n9\r\n7\r\n1\r\n0\r\n3\r\n6\r\n5\r\n9\r\n1\r\n1\r\n8\r\n0\r\n2\r\n4\r\n2\r\n9\r\n1\r\n5","sampleOutput":"9\r\n0\r\n3\r\n5\r\n8","questionVideo":"https://www.youtube.com/embed/KruULhITtk4","hints":[],"associated":[{"id":"17336c84-9ff5-40e8-958a-ed277b74e6d1","name":"What should be the output, if a1={1,1,1,2,2,3,4,5} and a2={1,1,2,2,2,4,4} ? (Get_Common_Elements-1)","slug":"what-should-be-the-output-if-a1-1-1-1-2-2-3-4-5-and-a2-1-1-2-2-2-4-4-get-common-elements-1","type":4},{"id":"59323466-bf82-4a69-a2c6-5a4ba9a7bed2","name":"Is hashmap an ordered collection? (Get_Common_Elements-1)","slug":"is-hashmap-an-ordered-collection-get-common-elements-1","type":4},{"id":"e310c393-1bb8-492d-927a-cab6094ef820","name":"What happens if we put a key object in a HashMap which already exists? (Get_Common_Elements-1)","slug":"what-happens-if-we-put-a-key-object-in-a-hashmap-which-already-exists-get-common-elements-1","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":"78ccb5a4-0b1a-4a23-9c66-ad59ab96fff8","name":"Get Common Elements - 1","slug":"get-common-elements-1","type":1}],"next":{"id":"f0638035-136e-4500-a53b-aef089fa6bef","name":"Get Common Elements-1","type":3,"slug":"get-common-elements-1"},"prev":{"id":"ba794a45-2b3c-4420-888c-e6796e40a0b9","name":"Highest Frequency Character","type":3,"slug":"highest-frequency-character"}}}`

# Get Common Elements - 1

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 print all elements of a2 which are also present in a1 (in order of their occurence in a2). Make sure to not print duplicates (a2 may have same value present many times).

`{"id":"e52a3f6d-53b2-4b1b-9b66-1926a2ee03f7","name":"Get Common Elements - 1","description":"1. You are given a number n1, representing the size of array a1.\r\n2. You are given n1 numbers, representing elements of array a1.\r\n3. You are given a number n2, representing the size of array a2.\r\n4. You are given n2 numbers, representing elements of array a2.\r\n5. You are required to print all elements of a2 which are also present in a1 (in order of their occurence in a2). Make sure to not print duplicates (a2 may have same value present many times).","inputFormat":"A number n1\r\nn1 number of elements line separated\r\nA number n2\r\nn2 number of elements line separated","outputFormat":"All relevant elements of a2 in separate lines (no duplicacy)","constraints":"1 &lt;= n1, n2 &lt;= 100\r\n0 &lt;= a1[i], a2[i] &lt; 10\r\nTime complexity should be O(n)","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <unordered_map>\nusing namespace std;\n\nint main() {\n int n1,n2;\n cin >> n1;\n int arr1[n1];\n \n for (int i = 0; i < n1; i++) {\n cin>>arr1[i];\n mp[arr1[i]]++;\n }\n cin >> n2;\n int arr2[n2];\n for (int i = 0; i < n2; i++) {\n cin >> arr2[i];\n \n // write your code here\n \n \n }\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"n1 = int(input())\narr1=[]\nfor x in range(n1):\n arr1.append(int(input()))\n\nn2 = int(input())\narr2=[]\nfor x in range(n2):\n arr2.append(int(input()))\n\n \n#write your code here"}},"points":10,"difficulty":"easy","sampleInput":"9\r\n5\r\n5\r\n9\r\n8\r\n5\r\n5\r\n8\r\n0\r\n3\r\n18\r\n9\r\n7\r\n1\r\n0\r\n3\r\n6\r\n5\r\n9\r\n1\r\n1\r\n8\r\n0\r\n2\r\n4\r\n2\r\n9\r\n1\r\n5","sampleOutput":"9\r\n0\r\n3\r\n5\r\n8","questionVideo":"https://www.youtube.com/embed/KruULhITtk4","hints":[],"associated":[{"id":"17336c84-9ff5-40e8-958a-ed277b74e6d1","name":"What should be the output, if a1={1,1,1,2,2,3,4,5} and a2={1,1,2,2,2,4,4} ? (Get_Common_Elements-1)","slug":"what-should-be-the-output-if-a1-1-1-1-2-2-3-4-5-and-a2-1-1-2-2-2-4-4-get-common-elements-1","type":4},{"id":"59323466-bf82-4a69-a2c6-5a4ba9a7bed2","name":"Is hashmap an ordered collection? (Get_Common_Elements-1)","slug":"is-hashmap-an-ordered-collection-get-common-elements-1","type":4},{"id":"e310c393-1bb8-492d-927a-cab6094ef820","name":"What happens if we put a key object in a HashMap which already exists? (Get_Common_Elements-1)","slug":"what-happens-if-we-put-a-key-object-in-a-hashmap-which-already-exists-get-common-elements-1","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":"78ccb5a4-0b1a-4a23-9c66-ad59ab96fff8","name":"Get Common Elements - 1","slug":"get-common-elements-1","type":1}],"next":{"id":"f0638035-136e-4500-a53b-aef089fa6bef","name":"Get Common Elements-1","type":3,"slug":"get-common-elements-1"},"prev":{"id":"ba794a45-2b3c-4420-888c-e6796e40a0b9","name":"Highest Frequency Character","type":3,"slug":"highest-frequency-character"}}}`

Editor

# Get Common Elements - 1

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 print all elements of a2 which are also present in a1 (in order of their occurence in a2). Make sure to not print duplicates (a2 may have same value present many times).

## 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 a2 in separate lines (no duplicacy)

## 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;}9 5 5 9 8 5 5 8 0 3 18 9 7 1 0 3 6 5 9 1 1 8 0 2 4 2 9 1 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;}9 0 3 5 8```

Question Video

Discussions

Show Discussion

Related Resources