Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
        mydb = new db();
        ArrayList list = new ArrayList();
        //List<datetime> list = new List<datetime>();
        string query1 = "select free_dates from I_mgr_free_dates where vacancy_id=" + "\'" + DropDownList1.SelectedItem + "\'";
        SqlDataReader read1 = mydb.getData(query1);
        while (read1.Read())
        {
            list.Add(Convert.ToDateTime(read1["free_dates"]));
        }

        mydb.CloseConnection();
        DateTime day1 = (DateTime)list[0];
        MessageBox.Show(day1.ToString());
</datetime></datetime>


i want to retrieve the most frequent element from the array list
Posted
Updated 8-Sep-11 2:54am
v2

Use order by desc in the select query and the first element of the array will be your latest entered value.
 
Share this answer
 
Comments
[no name] 8-Sep-11 9:40am    
+5 Most simple (assuming most 'frequent' means most recent.)
Please see my solution as I think performance wise finding the most recent should be quicker than sorting the collection.
Rob Philpott 8-Sep-11 11:52am    
Since when did frequent mean recent?! That's is one heck of an assumption.
Since you're getting it from a database anyway, let it do the work. It's good at this kind of thing. The query is something like this (may not quite be right, I'm not a database guy):

query = "select free_dates, count(free_dates) as number from I_mgr_free_dates where vacancy_id=" + "\'" + DropDownList1.SelectedItem + "\'" group by free_dates order by count desc limit 1";


Remember that if that dropdown list contains information entered by the user, pulled from an external configuration file etc you need to escape it to avoid SQL injection attacks. Use a parameterised query if possible.
 
Share this answer
 
Comments
Rob Philpott 8-Sep-11 11:55am    
+5. Clearly most answers here doesn't understand the concept of 'frequency'.
Rob Philpott 8-Sep-11 11:55am    
what's that 'limit 1' bit by the way? Some nasty Oraclism?
BobJanova 8-Sep-11 16:22pm    
Gets the first N rows (in this case 1). It works in mySQL, at least, and I think I've seen it done in SQL Server too.
Frequency as in the number of times a specific item appears or did you mean closest date to the current date?

If its the first then you'll have to setup a counter and increment each date that is matched or insert it into your match array/list. If its the second then you'll have to compare dates.
 
Share this answer
 
Hi,

You can use List of KeyValuePair (contains Keyword and count of it as pair) to count each element you read
to the most frequent element from it.

good luck
 
Share this answer
 
v2
I am not sure by what you mean as "the most frequent" but either way you can use LINQ.

(Side note.. You could use List[DateTime] instead of ArrayList... no casting then. I will use this for simplicity)

Assume you mean the most recent...
C#
List<datetime> list = new List<datetime>
...

var now = DateTime.Now;
var mostRecent = list.Min(date => now - date);

</datetime></datetime>


If you mean the "most common" then you can use LINQ to find the highest count.
DateTime highCountValue = null;
int count = int.MinValue;
list.ForEach(dateTime =>
{
    var potentialCount = list.Count(d => d == dateTime);
    if(potentialCount > count)
    {
        count = potentialCount;
        highCountValue = dateTime;
    }
});
 
Share this answer
 
C#
int count = 1;
                int currentIndex = 0;
                int i;
                for (i = 1; i < list.Count; i++)
                {
                    if (list[i] == list[currentIndex])
                        count++;
                    else
                        count--;
                    if (count == 0)
                    {
                        currentIndex = i;
                        count = 1;
                    }

                }

                DateTime day2 = (DateTime)list[currentIndex];
                MessageBox.Show(day2.ToString());


i used this code.but this gives last element of the array.

assume there is an array,
{1,3,4,1,3,1}
the most frequent element is 1.
i wanna get that out put.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900