How to Remove Duplicates from a C# Array

How do I remove duplicates from a C# array?

You could possibly use a LINQ query to do this:

int[] s = { 1, 2, 3, 3, 4};
int[] q = s.Distinct().ToArray();

Eliminate duplicates from array c#

Since initial array doesn't contain duplicates, you can sort it in random order and pick up 10 top items:

   Finalists1 = People1
.OrderByDescending(item => 1) // if people have some points, bonuses etc.
.ThenBy(item => Guid.NewGuid()) // shuffle among peers
.Take(10) // Take top 10
.ToArray(); // materialize as an array

If people are selected to the final are not completely random (e.g. contestant can earn points, bonuses etc.) change .OrderByDescending(item => 1), e.g.

     .OrderByDescending(item => item.Bonuses)

If you don't want to use Linq, you can just draw Peoples from urn without returning:

     private static Random random = new Random();  

...

List<People> urn = new List<People>(People1);

for (int i = 0; i < Finalists1.Length; ++i) {
int index = random.Next(0, urn.Count);

Finalists1[i] = urn[index];
urn.RemoveAt(index);
}

Remove duplicates from array of objects

With Linq, you can do this in O(n) time (single level loop) with a GroupBy

var uniquePersons = persons.GroupBy(p => p.Email)
.Select(grp => grp.First())
.ToArray();

Update

A bit on O(n) behavior of GroupBy.

GroupBy is implemented in Linq (Enumerable.cs) as this -

The IEnumerable is iterated only once to create the grouping. A Hash of the key provided (e.g. "Email" here) is used to find unique keys, and the elements are added in the Grouping corresponding to the keys.

Please see this GetGrouping code. And some old posts for reference.

  • What's the asymptotic complexity of GroupBy operation?
  • What guarantees are there on the run-time complexity (Big-O) of LINQ methods?

Then Select is obviously an O(n) code, making the above code O(n) overall.

Update 2

To handle empty/null values.

So, if there are instances where the value of Email is null or empty, the simple GroupBy will take just one of those objects from null & empty each.

One quick way to include all those objects with null/empty value is to use some unique keys at the run time for those objects, like

var tempEmailIndex = 0;
var uniqueNullAndEmpty = persons
.GroupBy(p => string.IsNullOrEmpty(p.Email)
? (++tempEmailIndex).ToString() : p.Email)
.Select(grp => grp.First())
.ToArray();

How to delete duplicate values in an array?

With LINQ it's easy:

var intArray = new[] { 2000, 2011, 2011, 2012, 2009, 2009, 2000 };
var uniqueArray = intArray.Distinct().ToArray();

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx

Another way is using Enumerable.GroupBy:

uniqueArray = intArray.GroupBy(i => i).Select(grp => grp.Key).ToArray();

Delete duplicates in a List of int arrays

Use GroupBy:

var result = intArrList.GroupBy(c => String.Join(",", c))
.Select(c => c.First().ToList()).ToList();

The result:

{0, 0, 0}

{20, 30, 10, 4, 6}

{1, 2, 5}

{12, 22, 54}

{1, 2, 6, 7, 8}

{0, 0, 0, 0}

EDIT: If you want to consider {1,2,3,4} be equal to {2,3,4,1} you need to use OrderBy like this:

var result = intArrList.GroupBy(p => string.Join(", ", p.OrderBy(c => c)))
.Select(c => c.First().ToList()).ToList();

EDIT2: To help understanding how the LINQ GroupBy solution works consider the following method:

public List<int[]> FindDistinctWithoutLinq(List<int[]> lst)
{
var dic = new Dictionary<string, int[]>();
foreach (var item in lst)
{
string key = string.Join(",", item.OrderBy(c=>c));

if (!dic.ContainsKey(key))
{
dic.Add(key, item);
}
}

return dic.Values.ToList();
}

C# remove duplicates from jagged array

You can solve it by using Except method from System.Linq namespace.

At every loop iteration go through all next rows and get the difference between these rows and current one and reassign it back. It's possible, because jagged array is an array whose elements are arrays and you have the same int data type for all items

int[][] jaggedArray = new int[4][];

jaggedArray[0] = new[] { 1 };
jaggedArray[1] = new[] { 11, 12 };
jaggedArray[2] = new[] { 3, 7, 11, 15 };
jaggedArray[3] = new[] { 6, 7, 10, 11 };

for (int i = 0; i < jaggedArray.Length; i++)
{
var currentRow = jaggedArray[i];
for (int j = i + 1; j < jaggedArray.Length; j++)
{
var result = jaggedArray[j].Except(currentRow);
jaggedArray[j] = result.ToArray();
}
}

If you print the result array

foreach (var array in jaggedArray)
{
foreach (var item in array)
Console.Write($"{item} ");

Console.WriteLine();
}

The output will be the following

result

Efficient way to remove duplicate strings from a string array in C#

You can use a HashSet:

string[] a = { "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };
var b = new HashSet<string>(a);

How to sort and remove duplicates from string Array without using the inbuilt function?


Here is what you can do

List<string> colorList = new List<string> { "Yellow", "Blue", "Green", "Red", "Blue", "Green", "Red", "Green", "Green", "Yellow" };
colorList = colorList.Distinct().OrderBy(item=> item).ToList();

File.ReadAllLines() will give you string[] and you can apply Dictinct and OrderBy in the same way as mentioned above

Sorting & remove duplicate without using build-in functions

Create a call to methods like this

string[] colorArray = new string[] { "Yellow", "Blue", "Green", "Red", "Blue", "Green", "Red", "Green", "Green", "Yellow" };
colorArray = RemoveDuplicates(colorArray);
Sort(colorArray);

sort using below method

static void Sort(string[] sa)
{
int pos = 1;
while (pos < sa.Length)
{
if (String.Compare(sa[pos], sa[pos - 1], StringComparison.OrdinalIgnoreCase) >= 0)
{
pos++;
}
else
{
string temp = sa[pos];
sa[pos] = sa[pos - 1];
sa[pos - 1] = temp;
if (pos > 1) pos--;
}
}
}

Remove duplicates using this one

static string[] RemoveDuplicates(string[] inputArray)
{

int length = inputArray.Length;
for (int i = 0; i < length; i++)
{
for (int j = (i + 1); j < length;)
{
if (inputArray[i] == inputArray[j])
{
for (int k = j; k < length - 1; k++)
inputArray[k] = inputArray[k + 1];
length--;
}
else
j++;
}
}

string[] distinctArray = new string[length];
for (int i = 0; i < length; i++)
distinctArray[i] = inputArray[i];

return distinctArray;

}

Removing duplicates from string array

Your code has two errors. The first is the missing call to ToList, as already pointed out. The second is subtle. Unique compares objects by identity, but your duplicate list items have are different array instances.

There are multiple solutions for that problem.

  • Use a custom equality comparer in moves.Distinct().ToList(). No further changes necessary.

    Sample implementation:

    class ArrayEqualityComparer<T> : EqualityComparer<T> {
    public override bool Equals(T[] x, T[] y) {
    if ( x == null ) return y == null;
    else if ( y == null ) return false;
    return x.SequenceEquals(y);
    }
    public override int GetHashCode(T[] obj) {
    if ( obj == null) return 0;
    return obj.Aggregate(0, (hash, x) => hash ^ x.GetHashCode());
    }
    }

    Filtering for unique items:

    moves = moves.Distinct(new ArrayEqualityComparer<string>()).ToList();
  • Use Tuple<string,string,string> instead of string[]. Tuple offers built-in structural equality and comparison. This variant might make your code cluttered because of the long type name.

    Instantiation:

    List<Tuple<string, string, string>> moves = 
    new List<Tuple<string, string, string>>();

    Adding new moves:

    Tuple<string, string, string> newmove = 
    Tuple.Create(piece, axis.ToString(), direction.ToString());
    moves.Add(newmove);

    Filtering for unique items:

    moves = moves.Distinct().ToList();
  • Use a custom class to hold your three values. I'd actually recommend this variant, because it makes all your code dealing with moves much more readable.

    Sample implementation:

    class Move {

    public Move(string piece, string axis, string direction) {
    Piece = piece;
    Axis = axis;
    Direction = direction;
    }

    string Piece { get; private set; }
    string Axis { get; private set; }
    string Direction { get; private set; }

    public override Equals(object obj) {
    Move other = obj as Move;
    if ( other != null )
    return Piece == other.Piece &&
    Axis == other.Axis &&
    Direction == other.Direction;
    return false;
    }

    public override GetHashCode() {
    return Piece.GetHashCode() ^
    Axis.GetHashCode() ^
    Direction.GetHashCode();
    }

    // TODO: override ToString() as well
    }

    Instantiation:

    List<Move> moves = new List<Move>();

    Adding new moves:

    Move newmove = new Move(piece, axis.ToString(), direction.ToString()); 
    moves.Add(newmove);

    Filtering for unique items:

    moves = moves.Distinct().ToList();


Related Topics



Leave a reply



Submit