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;

Lijo
  • 6,498
  • 5
  • 49
  • 60
  • How about Distinct() ?? – Rangesh Jul 06 '14 at 04:00
  • 1
    adding the example of what you wanted exactly should have been done initially... – Mitch Wheat Jul 06 '14 at 04:05
  • pleas be sure about the questions...its not just removing duplicates.. – Lijo Jul 06 '14 at 04:06
  • i have added the output i wanted..its different from the answer given in removing duplicates – Lijo Jul 06 '14 at 04:06
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 06 '14 at 05:40
  • var dupe = n.GroupBy(s => s) .SelectMany(grp => grp.Skip(1)).Distinct().ToArray(); int[] f = n.Except(dupe ).ToArray(); this is how i tried.. – Lijo Jul 06 '14 at 06:31

2 Answers2

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

Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
  • Congratulations, sir! This is way better :) – matiash Jul 06 '14 at 04:45
  • 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. – qbik Jul 06 '14 at 05:03
  • linq for getting the most repeated word in a string? – Lijo Jul 08 '14 at 09:39
  • @Lijo you should create a new question for this one. Comments are not an appropriate place. – Dmitrii Dovgopolyi Jul 08 '14 at 19:15
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.

Community
  • 1
  • 1
matiash
  • 54,791
  • 16
  • 125
  • 154
  • 1
    make sure you heed the bit that says: "It's quadratic though" – Mitch Wheat Jul 06 '14 at 04:12
  • can you explain i didnt got what you commented – Lijo Jul 06 '14 at 04:12
  • 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 – Lijo Jul 06 '14 at 04:14
  • @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. – matiash Jul 06 '14 at 04:15
  • I made an attempt at a slightly faster (alebit not compeltely pure LINQ) solution. – Vaughan Hilts Jul 06 '14 at 04:21