Find All Duplicates in an Array
Given an array of integers, find all elements that appear twice.
Function Signature
function findDuplicates(nums);
Example 1:
Input:
nums = [4,3,2,7,8,2,3,1]
Output:[2,3]
Explanation: The numbers 2 and 3 appear twice in the array.
Example 2:
Input:
nums = [1,1,2,3,3,4,4,4,5]
Output:[1,3,4]
Explanation: The numbers 1, 3, and 4 appear twice or more in the array.
Solve this problem
Start your 7-day free trial to solve this problem in our code editor with instant feedback.
Start 7-Day Free Trial to Solve This ProblemRelated Problems
Search in Rotated Sorted Array
hardWrite a function `search` that takes a rotated sorted array and a target value, and returns the index if the target is found. If not, returns -1. ## Function Signature ```function search(nums, target);``` ### Example 1: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0` \ > **Output:** `4` \ > **Explanation:** Target 0 is at index 4. ### Example 2: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3` \ > **Output:** `-1` \ > **Explanation:** Target 3 is not in the array.
ArrayTop K Frequent Elements
hardWrite a function `topKFrequent` that takes an array of integers and an integer k, and returns the k most frequent elements. ## Function Signature ```function topKFrequent(nums, k);``` ### Example 1: > **Input:** `nums = [1,1,1,2,2,3]`, `k = 2` \ > **Output:** `[1,2]` \ > **Explanation:** The two most frequent elements are 1 and 2. ### Example 2: > **Input:** `nums = [1]`, `k = 1` \ > **Output:** `[1]` \ > **Explanation:** The most frequent element is 1.
ArrayPermutations
hardWrite a function `permute` that takes an array of distinct integers and returns all possible permutations. ## Function Signature ```function permute(nums);``` ### Example 1: > **Input:** `nums = [1,2,3]` \ > **Output:** `[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]` \ > **Explanation:** The permutations of [1,2,3] are [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]. ### Example 2: > **Input:** `nums = [0,1]` \ > **Output:** `[[0,1],[1,0]]` \ > **Explanation:** The permutations of [0,1] are [[0,1],[1,0]]. ### Example 3: > **Input:** `nums = [1]` \ > **Output:** `[[1]]` \ > **Explanation:** The permutations of [1] are [[1]].
Array