Tuesday, 29 July 2014

Find the duplicates value in Array in C#

Finding the duplicate value in array using C# code.

class Program
    {
        static void Main(string[] args)
        {
            string InuputString = "Input Array \n";
             Console.WriteLine(InuputString);

             ArrayList obj = new ArrayList(){"1","4","Test","^","1","67","43","4","Ret","78"};

             for (int i = 0; i < obj.Count; i++)
             {
                 Console.Write(obj[i] + " ");
             }

             GetDuplicateArryValue(obj);
        }

        public static void GetDuplicateArryValue(ArrayList arr)
        {
            //to store all the duplicate values in Array list
            ArrayList dup = new ArrayList();

            for (int i = 0; i < arr.Count; i++)
            {
                for (int j = i + 1; j < arr.Count; j++)
                {
                    if (arr[i].Equals(arr[j]))
                    {
                        //When duplicate value is found, check
                        //whether the value not contained in the dup array list
                        if (!dup.Contains(arr[i]))
                        {
                            dup.Add(arr[i]);
                        }
                    }
                }
            }
            Console.WriteLine("\n Duplicates Number Are");

            for (int i = 0; i < dup.Count; i++)
            {
                Console.Write(dup[i] + " ");
            }
            Console.WriteLine();
        }
    }


OUTPUT :

No comments:

Post a Comment