# Duplicates Removal in Array using BST

• Difficulty Level : Easy
• Last Updated : 13 Aug, 2021

Given an array arr[] of integers, the task is to remove duplicates from the given array.

Examples

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.

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

Input: arr[] = {127, 234, 127, 654, 355, 789, 355, 355, 999, 654}
Output: arr[] = {127, 234, 355, 654, 789, 999}```

The duplicates in the array can be removed using Binary Search Tree. The idea is to create a Binary Search Tree using the array elements with the condition that the first element is taken as the root(parent) element and when the element “less” than root appears, it is made the left child and the element “greater” than root is made the right child of the root. Since no condition for “equal” exists the duplicates are automatically removed when we form a binary search tree from the array elements.

For the array, arr[] = {1, 2, 3, 2, 5, 4, 4}
BST will be:

Approach:

• Form BST using the array elements
• Display the elements using any Tree Traversal method.

Below is the implementation of the above approach.

## C++

 `// C++ Program of above implementation``#include ``using` `namespace` `std;` `// Struct declaration``struct` `Node {``    ``int` `data;``    ``struct` `Node* left;``    ``struct` `Node* right;``};` `// Node creation``struct` `Node* newNode(``int` `data)``{``    ``struct` `Node* nn``        ``= ``new` `Node;``    ``nn->data = data;``    ``nn->left = NULL;``    ``nn->right = NULL;``    ``return` `nn;``}` `// Function to insert data in BST``struct` `Node* insert(``struct` `Node* root, ``int` `data)``{``    ``if` `(root == NULL)``        ``return` `newNode(data);``    ``else` `{``        ``if` `(data < root->data)``            ``root->left = insert(root->left, data);``        ``if` `(data > root->data)``            ``root->right = insert(root->right, data);``        ``return` `root;``    ``}``}` `// InOrder function to display value of array``// in sorted order``void` `inOrder(``struct` `Node* root)``{``    ``if` `(root == NULL)``        ``return``;``    ``else` `{``        ``inOrder(root->left);``        ``cout << root->data << ``" "``;``        ``inOrder(root->right);``    ``}``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 2, 5, 4, 4 };` `    ``// Finding size of array arr[]``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``struct` `Node* root = NULL;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Insert element of arr[] in BST``        ``root = insert(root, arr[i]);``    ``}` `    ``// Inorder Traversal to print nodes of Tree``    ``inOrder(root);``    ``return` `0;``}` `// This code is contributed by shivanisingh`

## C

 `// C Program of above implementation``#include ``#include ` `// Struct declaration``struct` `Node {``    ``int` `data;``    ``struct` `Node* left;``    ``struct` `Node* right;``};` `// Node creation``struct` `Node* newNode(``int` `data)``{``    ``struct` `Node* nn``        ``= (``struct` `Node*)(``malloc``(``sizeof``(``struct` `Node)));``    ``nn->data = data;``    ``nn->left = NULL;``    ``nn->right = NULL;``    ``return` `nn;``}` `// Function to insert data in BST``struct` `Node* insert(``struct` `Node* root, ``int` `data)``{``    ``if` `(root == NULL)``        ``return` `newNode(data);``    ``else` `{``        ``if` `(data < root->data)``            ``root->left = insert(root->left, data);``        ``if` `(data > root->data)``            ``root->right = insert(root->right, data);``        ``return` `root;``    ``}``}` `// InOrder function to display value of array``// in sorted order``void` `inOrder(``struct` `Node* root)``{``    ``if` `(root == NULL)``        ``return``;``    ``else` `{``        ``inOrder(root->left);``        ``printf``(``"%d "``, root->data);``        ``inOrder(root->right);``    ``}``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 2, 5, 4, 4 };` `    ``// Finding size of array arr[]``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``struct` `Node* root = NULL;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Insert element of arr[] in BST``        ``root = insert(root, arr[i]);``    ``}` `    ``// Inorder Traversal to print nodes of Tree``    ``inOrder(root);``    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.Scanner;` `// Node declaration``class` `Node``{``    ``int` `data;``    ``public` `Node left;``    ``public` `Node right;``    ``Node(``int` `data)``    ``{``        ``this``.data = data;``        ``left = right = ``null``;``    ``}``}` `class` `GFG``{` `    ``// Function to insert data in BST``    ``public` `static` `Node insert(Node root, ``int` `data)``    ``{``        ``if` `(root == ``null``)``            ``return` `new` `Node(data);``        ``if` `(data < root.data)``            ``root.left = insert(root.left, data);``        ``if` `(data > root.data)``            ``root.right = insert(root.right, data);``        ``return` `root;``    ``}` `    ``// InOrder function to display value of array``    ``// in sorted order``    ``public` `static` `void` `inOrder(Node root)``    ``{``        ``if` `(root == ``null``)``            ``return``;``        ``inOrder(root.left);``        ``System.out.print(root.data+``" "``);``        ``inOrder(root.right);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String []args){``        ``int` `arr[] = { ``1``, ``2``, ``3``, ``2``, ``5``, ``4``, ``4` `};` `        ``// Finding size of array arr[]``        ``int` `n = arr.length;` `        ``Node root = ``null``;``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``// Insert element of arr[] in BST``            ``root = insert(root,arr[i]);``        ``}` `        ``// Inorder Traversal to print nodes of Tree``        ``inOrder(root);``    ``}``}` `// This code is contributed by anishma`

## Python3

 `# Python3 implementation of the approach` `# Binary tree node consists of data, a``# pointer to the left child and a``# pointer to the right child``class` `newNode :``    ``def` `__init__(``self``,data) :``        ``self``.data ``=` `data;``        ``self``.left ``=` `None``;``        ``self``.right ``=` `None``;` `# Function to insert data in BST``def` `insert(root, data) :` `    ``if` `(root ``=``=` `None``) :``        ``return` `newNode(data);``        ` `    ``else` `:``        ``if` `(data < root.data) :``            ``root.left ``=` `insert(root.left, data);``            ` `        ``if` `(data > root.data) :``            ``root.right ``=` `insert(root.right, data);``            ` `        ``return` `root;` `# InOrder function to display value of array``# in sorted order``def` `inOrder(root) :` `    ``if` `(root ``=``=` `None``) :``        ``return``;``        ` `    ``else` `:``        ``inOrder(root.left);``        ``print``(root.data, end ``=` `" "``);``        ``inOrder(root.right);``    ` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``1``, ``2``, ``3``, ``2``, ``5``, ``4``, ``4` `];` `    ``# Finding size of array arr[]``    ``n ``=` `len``(arr);` `    ``root ``=` `None``;` `    ``for` `i ``in` `range``(n) :` `        ``# Insert element of arr[] in BST``        ``root ``=` `insert(root, arr[i]);` `    ``# Inorder Traversal to print nodes of Tree``    ``inOrder(root);` `# This code is contributed by AnkitRai01`

## C#

 `// C# program of above implementation``using` `System;` `// Node declaration ``public` `class` `Node ``{ ``    ``public` `int` `data; ``    ``public` `Node left; ``    ``public` `Node right;``    ` `    ``public` `Node(``int` `data)``    ``{``        ``this``.data = data;``        ``left = right = ``null``;``    ``}``}``    ` `class` `GFG{``    ` `// Function to insert data in BST ``public` `static` `Node insert(Node root, ``int` `data)``{``    ``if` `(root == ``null``)``        ``return` `new` `Node(data);``    ``if` `(data < root.data) ``        ``root.left = insert(root.left, data); ``    ``if` `(data > root.data)``        ``root.right = insert(root.right, data);``        ` `    ``return` `root; ``} ` `// InOrder function to display value of array ``// in sorted order ``public` `static` `void` `inOrder(Node root)``{ ``    ``if` `(root == ``null``) ``        ``return``; ``        ` `    ``inOrder(root.left); ``    ``Console.Write(root.data + ``" "``);``    ``inOrder(root.right); ``}` `// Driver Code   ``static` `void` `Main()``{``    ``int``[] arr = { 1, 2, 3, 2, 5, 4, 4 }; ``    ` `    ``// Finding size of array arr[] ``    ``int` `n = arr.Length; ``    ` `    ``Node root = ``null``; ``    ``for``(``int` `i = 0; i < n; i++) ``    ``{ ``        ` `        ``// Insert element of arr[] in BST``        ``root = insert(root, arr[i]);``    ``} ``    ` `    ``// Inorder Traversal to print nodes of Tree ``    ``inOrder(root);``}``}` `// This code is contributed by divyeshrabadiya07`

## Javascript

 ``
Output:
`1 2 3 4 5`

Time Complexity:  in the worst case(when the array is sorted) where N is the size of the given array.
Auxiliary Space: O(N).

My Personal Notes arrow_drop_up