0

I have a list of string having currencies in it, for example the size is 1000. I want list if string of all the unique currencies out of 1000 records.

Now its like -

INR
USD
JPY
USD
USD
INR

and I want List of string like -

INR
USD
JPY

only unique record

Preferably without using Linq

nitendra jain
  • 433
  • 4
  • 8
  • 16

5 Answers5

4

EDIT:

I missed the part "preferably without using LINQ", You may try the following if you are using .Net framework 2.0 or you don't want to use LINQ.

List<string> list = new List<string> { "abc", "abc", "ab", "def", "abc", "def" };
list.Sort();
int i = 0;
while (i < list.Count - 1)
{
    if (list[i] == list[i + 1])
        list.RemoveAt(i);
    else
        i++;
}

use Distinct()

List<string> list = new List<string> { "abc", "abc", "ab", "def", "abc","def" };
List<string> uniqueList = list.Distinct().ToList();

The uniqueList will contain 3 items "abc","ab","def"

Remember to include: using System.Linq; at the top

Habib
  • 219,104
  • 29
  • 407
  • 436
  • -1 "*Preferably without using Linq*" – James Oct 08 '12 at 11:42
  • 1
    @James, missed the part for not using LINQ, have modified my answer accordingly – Habib Oct 08 '12 at 11:49
  • 2
    @Habib downvote revoked :) However, I would recommend using a `HashSet` over creating another list, sorting it etc (if possible, not sure which version of .NET the OP is on). – James Oct 08 '12 at 11:55
  • Caution: This modifies the original list and might not be what the OP intended but might aswell be exactly what the OP wants. – Sani Huttunen Oct 08 '12 at 12:00
1

HashSet<T> is what you're looking for. Reference MSDN:

The HashSet<T> class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

Note that the HashSet<T>.Add(T item) method returns a bool -- true if the item was added to the collection; false if the item was already present.

HashSet will work for you if using .NET 3.5 or above, no Linq involved.

var hash = new HashSet<string>();
var collectionWithDup = new [] {"one","one","two","one","two","zero"}; 

foreach (var str in collectionWithDup)
{
    hash.Add(str);
}

// Here hash (of type HashSet) will be containing the unique list

If you are not using .NET 3.5, simple use this piece of code:

List<string> newList = new List<string>();

foreach (string s in list)
{
   if (!newList.Contains(s))
      newList.Add(s);
}
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
0

Why not store these in a HashSet and then read out from that. The set will only contain unique values.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Yes, see this question: http://stackoverflow.com/questions/1388361/getting-all-unique-items-in-a-c-sharp-list – David Oct 08 '12 at 11:38
0

Suppose you have these values stored in array or ArrayList there are 2 ways:

first way

var UniqueValues = nonUnique.Distinct().ToArray();

second way

//create a test arraylist of integers
int[] arr = {1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 9};
ArrayList arrList = new ArrayList(arr);
//use a hashtable to create a unique list
Hashtable ht = new Hashtable();
foreach (int item in arrList) {
//set a key in the hashtable for our arraylist value - leaving the hashtable value      empty
    ht.Item[item] = null;
}
//now grab the keys from that hashtable into another arraylist
ArrayList distincArray = new ArrayList(ht.Keys);
ebram khalil
  • 8,252
  • 7
  • 42
  • 60
0

You can create your own Distinct Extension Method:

public static class ExtensionMethods
{
  public static IEnumerable<T> Distinct<T>(this IEnumerable<T> list)
  {
    var distinctList = new List<T>();

    foreach (var item in list)
    {
      if (!distinctList.Contains(item)) distinctList.Add(item);
    }

    return distinctList;
  }
}

Now you can do this:

static void Main(string[] args)
{
  var list = new List<string>() {"INR", "USD", "JPY", "USD", "USD", "INR"};
  var distinctList = list.Distinct();
  foreach(var item in distinctList) Console.WriteLine(item);
}

Which will yield:

INR
USD
JPY
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79