New update is available. Click here to update.

Last Updated: 1 Dec, 2020

Difficulty: Easy

```
The first line contains a single integer T representing the number of test cases.
The first line of each test case will contain the integer N.
The second and last line contains N space-separated integers that denote the elements of the given array.
```

```
For each test case, return the new array that does not contain any duplicates of the input array.
The output of each test case should be printed in a separate line.
```

```
You do not need to print anything; it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 10
1 <= N <= 5*10^3
-10^5 <= arr[i] <= 10^5
Time Limit: 1 sec
```

We will traverse the whole array and check if that element previously occurred or not.

The steps are as follows:

- We initialize a vector ‘ans’ to store the final non-duplicate elements.
- We will iterate over all the elements of the array, i.e., i = 0 to i = N - 1:
- We will iterate over all the elements of the array excluding present element, i.e., j = 0 to j = N - 1:
- If we get such positions such that arr[i] equals arr[j], then we replace all such arr[j] with -1.

- We will iterate over all the elements of the array excluding present element, i.e., j = 0 to j = N - 1:
- We will iterate over all the elements of the array, i.e., i = 0 to i = N - 1:
- If arr[i] not equals -1, then we store the value in ‘ans’.

- We will return ‘ans’ as the final answer.

We will traverse the whole array and keep a count of the frequency of each element. If the frequency is one or more than one, then we return only one occurrence of that element only.

The steps are as follows:

- We initialize a hash map ‘freqOfElements’ to store the frequency of each element and vector ‘ans’ to store the final non-duplicate elements.
- We will iterate over all the elements of the array, i.e., i = 0 to i = N - 1:
- We will store the frequency of an element arr[i] in ‘freqOfElements’.
- If the element already exists in ‘m’ then increment freqOfElements[arr[i]].

- We will iterate over all the elements of the array, i.e., i = 0 to i = N - 1:
- If freqOfElements[arr[i]] is not -1 then we store the value of arr[i] in ‘ans’.
- We change the value of freqOfElements[arr[i]] to -1.

- We will return ‘ans’ as the final answer.

SIMILAR PROBLEMS

Longest Subarray With Zero Sum

Posted: 3 Nov, 2022

Difficulty: Moderate

Merge Two Sorted Arrays Without Extra Space

Posted: 19 Nov, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Maximum GCD

Posted: 8 Dec, 2022

Difficulty: Hard

Negative To The End

Posted: 16 Dec, 2022

Difficulty: Easy

Popular Interview Problems: