Tutorialspoint
Problem
Solution
Submissions

Remove Duplicates from an Array

Certification: Basic Level Accuracy: 63.64% Submissions: 11 Points: 5

Write a C# function to remove duplicates from an array of integers and return the new array.

Example 1
  • Input: nums = [1, 2, 2, 3, 4, 4, 5, 5]
  • Output: [1, 2, 3, 4, 5]
  • Explanation:
    • Step 1: Create a data structure to keep track of unique elements (e.g., HashSet).
    • Step 2: Iterate through the input array [1, 2, 2, 3, 4, 4, 5, 5].
    • Step 3: Add each element to the HashSet, which automatically eliminates duplicates.
    • Step 4: Convert the HashSet to an array and return it.
    • Step 5: The result is [1, 2, 3, 4, 5], with all duplicates removed.
Example 2
  • Input: nums = [2, 2, 2, 2]
  • Output: [2]
  • Explanation:
    • Step 1: Create a data structure to keep track of unique elements (e.g., HashSet).
    • Step 2: Iterate through the input array [2, 2, 2, 2].
    • Step 3: Add each element to the HashSet, which automatically eliminates duplicates.
    • Step 4: Convert the HashSet to an array and return it.
    • Step 5: The result is [2], as all elements in the original array were duplicates of 2.
Constraints
  • 1 ≤ array length ≤ 10^4
  • -10^5 ≤ array elements ≤ 10^5
  • Time Complexity: O(n)
  • Space Complexity: O(n)
ArraysSetSnowflakeSamsung
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a hash set to store unique elements.
  • Traverse the array and add elements to the set if they are not already present.
  • Convert the set back to an array and return it.

Submitted Code :