![]() |
VOOZH | about |
Given an array arr[] of integers which may or may not contain duplicate elements. Your task is to remove duplicate elements. Your result should have elements according their first appearance in the input array.
Examples:
Input: arr[] = [1, 2, 3, 1, 4, 2]
Output: [1, 2, 3, 4]
Explanation: 2 and 1 have more than 1 occurence.Input: arr[] = [1, 2, 3, 4]
Output: [1, 2, 3, 4]
Explanation: There is no duplicate element.
Table of Content
Use two nested loops. The outer loop picks an element one by one starting from the leftmost element. The inner loop checks if the element is present on left side of it. If present, then ignore the element, else store it in result.
1 2 3 4
Use Hash Set to store distinct element. Insert all the elements in a hash set and then traverse the hash set to store the distinct elements in the resultant array.
1 2 3 4