Normally, we don't write up submissions that come from code websites or open source projects. This policy is partially because F/OSS projects are supposed to already have many eyes and partially because I don't want to get beat up. When the submitter is afraid of getting fired, anonymity kicks in. As for code websites that take themselves seriously, I just feel sorry for the people that write those.

That's why Alvin should be ashamed of himself, especially because this code and its coder aren't hurting him in any way. However, the snippet he sent in was just too good to pass up. I won't mention where the code came from but I have faith y'all'll figure it out.

The problem: in .NET, to take an array and find the unique values contained therein. The solution: tricky. (It has been formated to fit your screen and run in the allotted time.)

 

  static string [] strObj = {"A","A","B","C","C","D","E","F","B","C","G","F"};
  static Hashtable UniqueHT = new Hashtable();
  static int i =0;
  [STAThread]
  static void Main(string[] args)
  {      
      FindUniqueElements(0);          
  }

  private static void FindUniqueElements(int index)
  {          
      try
      {              
      for(i=index; i< strObj.Length; i++)
      {
      // Start adding the elements from array as value for KEY in HashTable
          UniqueHT.Add(strObj[i], "Any Value");
          Console.WriteLine(strObj[i]);
      }

      Console.ReadLine();                  
      }
      catch(Exception)
      {
      // When there is duplication of KEY value in HashTable, the exception
      // will be raised which can be handled in Catch block by just executing
      // the same function with next index as parameter.
          FindUniqueElements(i+1);              
      }
  }

 

Not just a hater, Alvin provides his own, less tricky solution:

  foreach(string s in strObj)
      if(! UniqueHT.ContainsKey(s))
          UniqueHT.Add(s, "A Value");

 

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!