An intermediate question for an interview. The question is to remove duplicates from array or, paraphrased for the interview, it would be: ‘Remove duplicates from a sorted array‘ or ‘Delete duplicates from a sorted array‘.
FREE : If you’re a new visitor preparing for an interview and need help or want to request a new question, you can use the form on the right side to ask your question. It’s free and doesn’t require signing in. Alternatively, you can ask in our Quora space, which is also free, and you’ll receive a response within 24 hours. Go ahead and check it out! do check. Solve Pass The Pillow Problem in C# With 2 Easy Algorithms , Master Minimize Maximum Difference in an Array in C# by 3 Easy Steps ,Master Abstract Factory Design Pattern for Programming Interviews with 5 easy steps ,Mastering Object-Oriented Programming in C++.
Table of Contents
Understand Logic Behind Filter Out Duplicate From Array
There are numerous ways to achieve this; however, today I’ll employ LINQ built-in functions to accomplish it. This choice is because in many interviews, you are permitted to use LINQ C# functions.
To delete duplicates from an array, the initial step involves finding duplicates, where I will utilize the Contains method of LINQ in C#. Once duplicates are identified, we will exclude those elements from the new sorted or unsorted array. By doing so, all duplicates will be removed from the collection data types.
C# Program To Remove Duplicates From Array
As I previously mentioned, there are numerous methods to achieve this. If you desire more examples for removing duplicates from an array, feel free to comment below. Alternatively, you can join our Quora community; it’s free, and you’ll receive answers within 48 hours. Let’s dive in to program.
If you’re a regular visitor, you’ve likely implemented our fundamental logic already. For newcomers, it’s essential to begin by crafting a clear logic blueprint and defining variables accordingly. Rushing to write code in an interview should be avoided; remember, the initial impression matters greatly. Pay close attention to naming conventions to maintain clarity and professionalism.
Step 1
In our scenario, I’ve instantiated an integer array named questionArray
and developed a static method with an integer array parameter and an integer array return type. Inside the method, I’ve initialized another integer array named result
and returned it.
static void Main(string[] args)
{
int[] questionArray = new int[] { 1, 2, 2, 4, 5, 6, 6, 9, 10, 12, 12 };
foreach (var item in RemoveDuplicates(questionArray))
{
Console.WriteLine(item);
}
}
private static int[] RemoveDuplicates(int[] questionArray)
{
List<int> result = new List<int>();
return result.ToArray();
}
Step 2
Now, utilize a foreach loop to iterate through each element, and employ the LINQ Contains
method to check if the current element is already present in the ‘result’ array. If it is, skip it; if not, add it to ‘result’.
private static int[] RemoveDuplicates(int[] questionArray)
{
List<int> result = new List<int>();
foreach (int num in questionArray)
{
if (!result.Contains(num))
{
result.Add(num);
}
}
return result.ToArray();
}
Step 3
Now execute your program and inspect the ‘result’ array. Your program to remove duplicates from the array is now complete.
Summarizing Delete Duplicates From Array
In this approach to remove duplicates from an array, we start by defining a method that takes an integer array as input and returns another array. Within this method, we initialize an empty result array and iterate through each element of the input array using a foreach loop. For each element, we check if it already exists in the result array using LINQ’s Contains method. If it doesn’t exist, we add it to the result array. Finally, we return the result array. This method ensures that duplicate elements are excluded, resulting in a unique array.