Array ( [0] => [1] => questions [2] => Arrays [3] => Removing-Duplicates ) Arrays | Removing Duplicates | THE INQUISITIVE





Removing Duplicates

LEVEL:Beginner

Description

Given list of elements. You have to remove the duplicate elements in given array and print the array.

Input Format

First line contains a number N. Next line contains N integers separated by spaces.

Output Format

Print the resultant array.


Example 1:

Input
5
3 5 4 4 1
Output
3 5 4 1
Example 2:

Input
8
2 5 4 3 4 1 2 7
Output
2 5 4 3 1 7
Example 3:

Input
10
9 3 8 4 7 5 9 2 3 8
Output
9 3 8 4 7 5 2

oops

Login to see Discussion




Approach

Approach 1: using Brute Force

Step-i) create a temp array of the given array size
Step-ii) initialize all the values with true
Step-iii) run a loop from i=0 to iStep-iv) if at current index temp is true go to next step
Step-v) run a loop from j=i+1 to jStep-vi) if array value at i and j are same then change the value of temp at j to false
Step-vii) create an empty string
Step-viii) run a loop form i = 0 to iStep-ix) if current temp value is true then append that index value from given array to string
Step-x) return the string

Time Complexity: O(n^2)
Space Complexity: O(n)

Approach 2: using set data structure

Step-i)create a empty set
Step-ii) add all elements of array to the set
Step-iii) now you can either return the set or create an empty string and add set elements to it and return it as done below in
the given example

Time Complexity: O(n)
Space Complexity: O(n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution