Sort elements by frequency

Print the elements of an array in the order of decreasing frequency. If two numbers have the same frequencies then print the one which came first.
E.g. 2 5 2 8 5 6 8 8 Output: 8 8 8 2 2 5 5 6.
E.g. 5 2 2 8 5 6 8 8 Output: 8 8 8 5 5 2 2 6.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           //Sort elements by frequency
            int[] numArray = { 5, 2, 2, 8, 5, 6, 8, 8 };
            Console.WriteLine("Initial Array");
            //Printing the initial array
            for (int i = 0; i < numArray.Length; i++)
            {
                Console.Write(numArray[i] + " ");
            }
            Console.WriteLine();
            ArrayList number = new ArrayList();
            ArrayList frequencyCount = new ArrayList();
            int count = 0;
            int[] tempArray = new int[numArray.Length];
            for (int i = 0; i < numArray.Length; i++)
            {
                tempArray[i] = numArray[i];
            }
            Array.Sort(numArray);
            for (int i = 0; i < numArray.Length; i++)
            {
                if (i < numArray.Length - 1)
                {
                    if (numArray[i] == numArray[i + 1])
                    {
                        count++;
                    }
                    else
                    {
                        number.Add(numArray[i]);
                        frequencyCount.Add(++count);
                        count = 0;
                    }
                }
                else
                {
                    number.Add(numArray[i]);
                    frequencyCount.Add(++count);
                    count = 0;
                }
            }
            //Sorting the frequency count
            for (int i = 1; i < frequencyCount.Count; i++)
            {
                for (int j = 0; j < frequencyCount.Count - i; j++)
                {
                    if ((int)frequencyCount[j] < (int)frequencyCount[j + 1])
                    {
                        int temp = (int)frequencyCount[j];
                        frequencyCount[j] = frequencyCount[j + 1];
                        frequencyCount[j + 1] = temp;
                        temp = (int)number[j];
                        number[j] = number[j + 1];
                        number[j + 1] = temp;
                    }
                    else if ((int)frequencyCount[j] == (int)frequencyCount[j + 1])
                    {
                        if (getIndex(tempArray,(int)number[j]) > getIndex(tempArray, (int)number[j + 1]))
                        {
                            int temp = (int)number[j];
                            number[j] = number[j + 1];
                            number[j + 1] = temp;
                        }
                    }
                }
            }
            Console.WriteLine("Sorted Array Based on Frequency");
            //Printing the processed array
            for (int i = 0; i < frequencyCount.Count; i++)
            {
                for (int j = 0; j < (int)frequencyCount[i]; j++)
                {
                    Console.Write((int)number[i] + " ");
                }
            }
            Console.WriteLine();
        }
 
        public static int getIndex(int[] tempArray, int num)
        {
            for (int i = 0; i < tempArray.Length; i++)
            {
                if (tempArray[i] == num)
                {
                    return (i);
                }
            }
            return (-1);
        }
    }
}

Output
=======
Initial Array
5 2 2 8 5 6 8 8
Sorted Array Based on Frequency
8 8 8 5 5 2 2 6
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

4 Responses to Sort elements by frequency

  1. how to sort 2-d array with index and value such that if two values is same then the value with lower index is printed first ?
    in nlogn
    i think ur code complexity is 0(n^2)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s