0

I want to remove all the duplicates in this array and I can't quite make it work. When all the duplicates are removed, I want to create a new array without the removed numbers. Here's my code:

  static void Main(string[] args)
    {
        int[] s = { 11, 11, 12, 12, 13, 13, 14, 15, 16 };
        int[] q = s.Distinct().ToArray();

        Console.WriteLine(q.ToString());
        Console.ReadLine();
    }

This prints the array {11, 12, 13, 14, 15, 16}; but I wanted it to print the array {14, 15, 16}.

5
  • 4
    You have successfully created an array without any duplicates. Well done /patsback. Commented Oct 27, 2014 at 17:12
  • 2
    q.ToString() is pointless if you don't want to know the full name of the array-type. Use string.Join(",", q) instead. Commented Oct 27, 2014 at 17:14
  • @BradleyDotNET Yes. And then I want to write it out in my console application. Commented Oct 27, 2014 at 17:14
  • 1
    new [] { 11, 11 }.Distinct().ToArray() returns int[]{ 11 }. It looks like you're expecting it to return an empty set. Commented Oct 27, 2014 at 17:18
  • 2
    A quick suggestion for future questions: include what you expect the output to be, in other words, say whats wrong with the code provided. My guess what just that, and what you were looking for was not obvious given the question. Commented Oct 27, 2014 at 17:21

4 Answers 4

2

Use:

int[] s = { 11, 11, 12, 12, 13, 13, 14, 15, 16 };
var NotDuplicateItems = s.GroupBy(r => r)
    .Where(grp => grp.Count() == 1)
    .Select(r => r.Key)
    .ToArray();

The above will give you items which are not duplicate in the array s

Sign up to request clarification or add additional context in comments.

Comments

1

If you know your input array is going to be sorted (it is in your example), you should take advantage of that. If you do, you can iterate over the array once:

public int[] RemoveDuplicates(int[] source)
{
    bool occurredOnce = true;
    int currentItem = source[0];

    var result = new List<int>();

    for (int i = 1; i < source.Length; i++)
    {
        if (source[i] != currentItem)
        {
            if (occurredOnce)
            {
                result.Add(currentItem);
            }

            currentItem = source[i];
            occurredOnce = true;
        }
        else 
        {
            occurredOnce = false;
        }
    }

    if (occurredOnce)
    {
        result.Add(currentItem);
    }

    return result.ToArray();
}

Example: https://dotnetfiddle.net/4qgEFs

1 Comment

@BradleyDotNET: Mmm, don't think so. When the first 12 is encountered, occurredOnce will be false-- the last iteration evaluated the second 11. The if block that would add 11 gets skipped, and currentItem and occurredOnce get reset. On the subsequent iteration, where currentItem is equal to 12, the occurredOnce flag will be set to false. I updated my answer with a working example.
1

Distinct returns a collection with all duplicate entries removed. It does not, however, remove the original items (that were later duplicated).

To remove all items that are duplicated, that is, that have a "Count" greater than 1:

int[] q = s.Where(i => s.Count(j => j == i) == 1).ToArray();

This is O(n^2), so don't do it over a large collection.

1 Comment

Not the downvoter, but it's probably because this code won't compile as written (see the q inside the lambda [you mean s, but you typoed it]).
-1

I cant code it at the moment.. but here is my thought process....

Take the first number, then remove it from the rest of the string (or array).

Then search for that number that was just removed in the string (or array).

If it finds it again, it wont print the number. If it doesnt - then the number is shown.

1 Comment

That works ok if you are just printing, but getting the final collection, then printing it, would be much more useful generally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.