{"id":"ad45ed74-aa7c-4e9a-a832-764e90b75e3c","name":"Find All Duplicates In An Array","description":"1. Given an integer array nums of length n where all the integers of nums are in the range [1, n].\r\n2. Each integer appears once or twice, return an array of all the integers that appears twice.\r\n3. You must write an algorithm that runs in O(n) time and uses only constant extra space.\r\n","inputFormat":"Input is Managed for you\r\n1. nums = [4,3,2,7,8,2,3,1]\r\n2. nums = [1]","outputFormat":"Output is Managed for you\r\n1. [2,3]\r\n2. [], Empty","constraints":"1. n == nums.length\r\n2. 1 &lt;= n &lt;= 100000\r\n3. 1 &lt;= nums[i] &lt;= n\r\n4. Each element in nums appears once or twice.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> findDuplicates(vector<int>& nums)\n{\n //write your code here\n}\n\nint main()\n{\n int n;\n cin>>n;\n vector<int> arr(n);\n \n for(int i=0; i<n ;i++)\n {\n cin>>arr[i];\n }\n \n \n vector<int> r = findDuplicates(arr);\n \n if(r.size() == 0)\n {\n cout<<\"Empty\"<<endl;\n return 0;\n }\n sort(r.begin(), r.end());\n \n for(int val : r)\n {\n cout<<val<<\" \";\n }\n cout<<endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~~~~~~\r\n public static List<Integer> findDuplicates(int[] nums) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n List<Integer> res = findDuplicates(arr);\r\n if (res.size() == 0) {\r\n System.out.println(\"Empty\");\r\n return;\r\n }\r\n Collections.sort(res);\r\n for (int val : res)\r\n System.out.print(val + \" \");\r\n System.out.println();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8\r\n4 3 2 7 8 2 3 1","sampleOutput":"2 3 ","questionVideo":"","hints":[],"associated":[],"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":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"6dfdfab5-494b-4509-b436-1368524d8c13","name":"Find All Duplicates In An Array","slug":"find-all-duplicates-in-an-array","type":1}],"next":{"id":"3a8c32aa-1bc8-4286-b72c-7ce06d8ca7d8","name":"Find All duplicates in an array","type":3,"slug":"find-all-duplicates-in-an-array"},"prev":{"id":"fbe32dc1-4123-4cc9-a2f9-6d21a713af1d","name":"Meeting Rooms II MCQ","type":0,"slug":"meeting-rooms-ii-mcq"}}}

Find All Duplicates In An Array

1. Given an integer array nums of length n where all the integers of nums are in the range [1, n]. 2. Each integer appears once or twice, return an array of all the integers that appears twice. 3. You must write an algorithm that runs in O(n) time and uses only constant extra space.

{"id":"ad45ed74-aa7c-4e9a-a832-764e90b75e3c","name":"Find All Duplicates In An Array","description":"1. Given an integer array nums of length n where all the integers of nums are in the range [1, n].\r\n2. Each integer appears once or twice, return an array of all the integers that appears twice.\r\n3. You must write an algorithm that runs in O(n) time and uses only constant extra space.\r\n","inputFormat":"Input is Managed for you\r\n1. nums = [4,3,2,7,8,2,3,1]\r\n2. nums = [1]","outputFormat":"Output is Managed for you\r\n1. [2,3]\r\n2. [], Empty","constraints":"1. n == nums.length\r\n2. 1 &lt;= n &lt;= 100000\r\n3. 1 &lt;= nums[i] &lt;= n\r\n4. Each element in nums appears once or twice.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> findDuplicates(vector<int>& nums)\n{\n //write your code here\n}\n\nint main()\n{\n int n;\n cin>>n;\n vector<int> arr(n);\n \n for(int i=0; i<n ;i++)\n {\n cin>>arr[i];\n }\n \n \n vector<int> r = findDuplicates(arr);\n \n if(r.size() == 0)\n {\n cout<<\"Empty\"<<endl;\n return 0;\n }\n sort(r.begin(), r.end());\n \n for(int val : r)\n {\n cout<<val<<\" \";\n }\n cout<<endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~~~~~~\r\n public static List<Integer> findDuplicates(int[] nums) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n List<Integer> res = findDuplicates(arr);\r\n if (res.size() == 0) {\r\n System.out.println(\"Empty\");\r\n return;\r\n }\r\n Collections.sort(res);\r\n for (int val : res)\r\n System.out.print(val + \" \");\r\n System.out.println();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8\r\n4 3 2 7 8 2 3 1","sampleOutput":"2 3 ","questionVideo":"","hints":[],"associated":[],"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":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"6dfdfab5-494b-4509-b436-1368524d8c13","name":"Find All Duplicates In An Array","slug":"find-all-duplicates-in-an-array","type":1}],"next":{"id":"3a8c32aa-1bc8-4286-b72c-7ce06d8ca7d8","name":"Find All duplicates in an array","type":3,"slug":"find-all-duplicates-in-an-array"},"prev":{"id":"fbe32dc1-4123-4cc9-a2f9-6d21a713af1d","name":"Meeting Rooms II MCQ","type":0,"slug":"meeting-rooms-ii-mcq"}}}
plane

Editor


Loading...

Find All Duplicates In An Array

easy

1. Given an integer array nums of length n where all the integers of nums are in the range [1, n]. 2. Each integer appears once or twice, return an array of all the integers that appears twice. 3. You must write an algorithm that runs in O(n) time and uses only constant extra space.

Constraints

1. n == nums.length 2. 1 <= n <= 100000 3. 1 <= nums[i] <= n 4. Each element in nums appears once or twice.

Format

Input

Input is Managed for you 1. nums = [4,3,2,7,8,2,3,1] 2. nums = [1]

Output

Output is Managed for you 1. [2,3] 2. [], Empty

Example

Sample Input

8 4 3 2 7 8 2 3 1

Sample Output

2 3

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode