Tuesday, October 11, 2011

Get unique value from Array (String/Integer):

Problem: In one of my project, I am getting values from one datasource in the form of .net Array. In the .NET Framework 3.5 there is a function called Distinct(). This Distinct() function return unique values from Array. This function works fine in the case of integer array. In string array, it not able to handle case sensitivity. If all items in the string array in same case, then it will work fine. Due to this, I was facing problem in getting unique value from String Array. Resolution: To Solve this problem, I have created functions, which used Stringcomparison. InvariantCultureIgnoreCase. .NET Framework 3.5: It is one line of code.
//If integer array
int [] intArr={5,2,3,3,5};
intArr = intArr.Distinct().ToArray(); // Output : intArr ={5,2,3}
//If string array
string [] strArr={“Amit”,”Amit”,”Kumar”,”Kumar”};
strArr = strArr.Distinct().ToArray(); //Output: strArr ={“Amit”,,”Kumar” }
string [] strArrCase={“Amit”,”AMIT”,”Kumar”,”KUMAR”,”Amit”,”Kumar”};
strArrCase = strArrCase.Distinct().ToArray(); //Output: strArrCase ={“Amit”,”AMIT”,”Kumar”,”KUMAR” }
Custom Function:
//Function call
string [] strArr={“Amit”,”Amit”,”Kumar”,”Kumar”};
strArr= GetUniqueArray(strArr); //Output: strArr ={“Amit”,”Kumar” }
        #region Get unique From Array
        public static string[] GetUniqueArray(string[] itemValues)
        {
            string stringList=string.Empty;
            string currentValue=string.Empty;
            string[] newArray=null;
            ArrayList uniqueList = new ArrayList();
            try
            {
                for (int counter = 0; counter < itemValues.Length; counter++)
                {
                    currentValue = Convert.ToString(itemValues[counter]);
                    if (!CheckDuplicateArray(currentValue, uniqueList))
                    {
                       uniqueList.Add(itemValues[counter]);
                    }
                }
                if (uniqueList.Count != 0)
                {
                    newArray = uniqueList.ToArray(Type.GetType("System.String")) as string[]; 
                }
            }
            catch (Exception ex)
            {
            }
            return newArray;
        }
        #endregion

        #region Check Duplicates From Array
        public static bool CheckDuplicateArray(string value, ArrayList uniqueList)
        {
            bool itemFound = false;
            try
            {
                foreach (string currentValue in uniqueList)
                {
                    if (string.Equals(value, currentValue, StringComparison.InvariantCultureIgnoreCase))
                    {
                        itemFound = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return itemFound;
        }
        #endregion
Share:

0 comments:

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels