3
int [] n=new int[10]{2,3,33,33,55,55,123,33,88,234};
output=2,3,123,88,234;

use LINQ i can do it using two for loops by continuously checking.but i need a more simple way using LINQ

its not removing duplicates.. removing duplicates by distinct will give = 2,3,123,33,55,88,234 my output should be = 2,3,123,,88,234;

6
  • How about Distinct() ?? Commented Jul 6, 2014 at 4:00
  • 1
    adding the example of what you wanted exactly should have been done initially... Commented Jul 6, 2014 at 4:05
  • pleas be sure about the questions...its not just removing duplicates.. Commented Jul 6, 2014 at 4:06
  • i have added the output i wanted..its different from the answer given in removing duplicates Commented Jul 6, 2014 at 4:06
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Jul 6, 2014 at 5:40

2 Answers 2

6

I combined your grouping idea and matiash's count. Not sure about its speed.

var result = n.GroupBy(s => s).Where(g => g.Count() == 1).Select(g => g.Key);

Update: i have measured the speed and it seems the time is linear, so you can use it on large collections

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

4 Comments

Congratulations, sir! This is way better :)
GroupBy should have O(n) complexity (see this question, it refers to SQL Group By operation, but the principle is the same). Filtering and selecting will also be O(n). One note: when testing the performance of answers (and Linq queries in general), be sure to call ToArray() or ToList() at the end. Most of Linq methods use yield internally, so the real computation can be deffered until you do an enumeration on the result. Calling ToArray() makes sure this enumeration happens and you measure the full running time.
linq for getting the most repeated word in a string?
@Lijo you should create a new question for this one. Comments are not an appropriate place.
4
var result = n.Where(d => n.Count(d1 => d1 == d) <= 1);

This reads: only take those elements that are present at most 1 times in n.

It's quadratic though. Doesn't matter for short collections, but could possibly be improved.

EDIT Dmitry's solution is linear, and hence far better.

5 Comments

make sure you heed the bit that says: "It's quadratic though"
can you explain i didnt got what you commented
its working and this what i needed..oh.. i think if we use this for large collections it will be slow that what you mean
@404 it means that the time needed will increase as the square of the number of items. In particular, for an input twice as large, it will take four times as long. So for big collections it could be unwieldly.
I made an attempt at a slightly faster (alebit not compeltely pure LINQ) solution.

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.