Related Articles

# Find missing elements from an Array

• Difficulty Level : Medium
• Last Updated : 15 Mar, 2021

Given a list of integers from the range [1, N] with some of the elements missing. The task is to find the missing elements. Note that there can be duplicates in the list.
Examples:

```Input: arr[] = {1, 3, 3, 3, 5}
Output: 2 4

Input: arr[] = {1, 2, 3, 4, 4, 7, 7}
Output: 5 6```

Approach: In the given range [1, N] there should be an element corresponding to each index. If an element is missing then its index will never be visited.

```Traverse the array:
For each element:
if array[element] > 0:
Mark the element as visited
Again, traverse the array:
if element isNot Visited:

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to find the missing elements``vector<``int``> missing_elements(vector<``int``> vec)``{` `    ``// Vector to store the list``    ``// of missing elements``    ``vector<``int``> mis;` `    ``// For every given element``    ``for` `(``int` `i = 0; i < vec.size(); i++) {` `        ``// Find its index``        ``int` `temp = ``abs``(vec[i]) - 1;` `        ``// Update the element at the found index``        ``vec[temp] = vec[temp] > 0 ? -vec[temp] : vec[temp];``    ``}``    ``for` `(``int` `i = 0; i < vec.size(); i++)` `        ``// Current element was not present``        ``// in the original vector``        ``if` `(vec[i] > 0)``            ``mis.push_back(i + 1);` `    ``return` `mis;``}` `// Driver code``int` `main()``{``    ``vector<``int``> vec = { 3, 3, 3, 5, 1 };` `    ``// Vector to store the returned``    ``// list of missing elements``    ``vector<``int``> miss_ele = missing_elements(vec);` `    ``// Print the list of elements``    ``for` `(``int` `i = 0; i < miss_ele.size(); i++)``        ``cout << miss_ele[i] << ``" "``;` `    ``return` `0;``}`

## Java

 `// Java implementation of the above approach``import` `java.util.*;` `class` `GFG``{``    ` `    ``// Function to find the missing elements``    ``static` `Vector missing_elements(Vector vec)``    ``{``    ` `        ``// Vector to store the list``        ``// of missing elements``        ``Vector mis = ``new` `Vector();``    ` `        ``// For every given element``        ``for` `(``int` `i = ``0``; i < vec.size(); i++)``        ``{``    ` `            ``// Find its index``            ``int` `temp = Math.abs((``int``)vec.get(i)) - ``1``;``    ` `            ``// Update the element at the found index``            ``if` `((``int``)vec.get(temp) > ``0``)``                ``vec.set(temp,-(``int``)vec.get(temp));``            ``else``                ``vec.set(temp,vec.get(temp));``        ``}``        ``for` `(``int` `i = ``0``; i < vec.size(); i++)``        ``{``            ``// Current element was not present``            ``// in the original vector``            ``if` `((``int``)vec.get(i) > ``0``)``                ``mis.add(i + ``1``);``        ``}``        ``return` `mis;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``Vector vec = ``new` `Vector();``        ``vec.add(``3``);``        ``vec.add(``3``);``        ``vec.add(``3``);``        ``vec.add(``5``);``        ``vec.add(``1``);``        ` `        ``// Vector to store the returned``        ``// list of missing elements``        ``Vector miss_ele = missing_elements(vec);``    ` `        ``// Print the list of elements``        ``for` `(``int` `i = ``0``; i < miss_ele.size(); i++)``            ``System.out.print(miss_ele.get(i) + ``" "``);``    ``}``}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 implementation of the approach` `# Function to find the missing elements``def` `missing_elements(vec):` `    ``# Vector to store the list``    ``# of missing elements``    ``mis ``=` `[]` `    ``# For every given element``    ``for` `i ``in` `range``(``len``(vec)):` `        ``# Find its index``        ``temp ``=` `abs``(vec[i]) ``-` `1` `        ``# Update the element at the found index``        ``if` `vec[temp] > ``0``:``            ``vec[temp] ``=` `-``vec[temp]` `    ``for` `i ``in` `range``(``len``(vec)):` `        ``# Current element was not present``        ``# in the original vector``        ``if` `(vec[i] > ``0``):``            ``mis.append(i ``+` `1``)` `    ``return` `mis` `# Driver code``vec ``=` `[``3``, ``3``, ``3``, ``5``, ``1``]` `# Vector to store the returned``# list of missing elements``miss_ele ``=` `missing_elements(vec)` `# Print the list of elements``for` `i ``in` `range``(``len``(miss_ele)):``    ``print``(miss_ele[i], end ``=` `" "``)` `# This code is contributed by Mohit Kumar`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Collections.Generic;            ` `class` `GFG``{``    ` `    ``// Function to find the missing elements``    ``static` `List<``int``> missing_elements(List<``int``> vec)``    ``{``    ` `        ``// List to store the list``        ``// of missing elements``        ``List<``int``> mis = ``new` `List<``int``>();``    ` `        ``// For every given element``        ``for` `(``int` `i = 0; i < vec.Count; i++)``        ``{``    ` `            ``// Find its index``            ``int` `temp = Math.Abs((``int``)vec[i]) - 1;``    ` `            ``// Update the element at the found index``            ``if` `((``int``)vec[temp] > 0)``                ``vec[temp] = -(``int``)vec[temp];``            ``else``                ``vec[temp] = vec[temp];``        ``}``        ``for` `(``int` `i = 0; i < vec.Count; i++)``        ``{``            ``// Current element was not present``            ``// in the original vector``            ``if` `((``int``)vec[i] > 0)``                ``mis.Add(i + 1);``        ``}``        ``return` `mis;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main(String []args)``    ``{``        ``List<``int``> vec = ``new` `List<``int``>();``        ``vec.Add(3);``        ``vec.Add(3);``        ``vec.Add(3);``        ``vec.Add(5);``        ``vec.Add(1);``        ` `        ``// List to store the returned``        ``// list of missing elements``        ``List<``int``> miss_ele = missing_elements(vec);``    ` `        ``// Print the list of elements``        ``for` `(``int` `i = 0; i < miss_ele.Count; i++)``            ``Console.Write(miss_ele[i] + ``" "``);``    ``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`2 4`

Time Complexity: O(N), where N is the length of the vector vec.

Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up