Question

What I am looking for is to have the duplicates removed from idFAV and then put it into a Text field. Any ideas? I am working on C# 2.0.

// main.cs

    public void Page_Load(object sender, EventArgs e)
    {
        Session["FAVisible"] = new ArrayList();
    }

// share.cs

    public void Page_Load(object sender, EventArgs e)
             {
               ArrayList idFAV = (ArrayList)Session["FAVisible"];
               idFAV.Add(FileName));
               Session["FAVisible"] = idFAV;

               for(int i=0; i < DisplayCount; i++) 
               {
                     FileAttachedVisible.Text = idFAV[i].ToString();
               }

              }
Was it helpful?

Solution

If this is all the places you are using ArrayList, then simplest fix is to check if the idFAV contains the FileName before adding it and you will never have duplicates. Or if this array list is being modified somewhere, then you can make a new Arraylist and loop through all the items in idFAV and add only if its not present in the new list.

OTHER TIPS

You don't need to remove if you check it before add... For example:

           foreach (var iMeter in meter)
              {
                   if (!ip.Contains(iMeter.IP))
                    {
                          ip.Add(iMeter.IP);
                      }
              }

You can try building new ArrayList and checking if any new element to be added is already in that list.

ArrayList result = new ArrayList();

foreach(var elem in idFav)
{
   if (!result.Contains(elem))
   {
       result.Add(elem);
   }
}

Of course, performance of this solution is not the best (O(n^2)). It would be better to try to play with Hashsets/dicitonaries and do this in O(n).

Also, for this to work objects in ArrayList need to have Equals implemented. If FileName is string you don't need to worry.

Here is one way to accomplish the task. I am sure it can be simplified but I wanted to lay out each step in the process.

// Build ArrayList
ArrayList al = new ArrayList();
al.Add(1);
al.Add(1);
al.Add(2);
al.Add(2);
al.Add(3);
al.Add(4);
al.Add(4);
al.Add(4);

// Create List from ArrayList
List<object> oList = new List<object>(al.ToArray());

// Declare holder to distinct values
List<object> distinctValues = new List<object>();

// Get distinct values
oList.ForEach(delegate(object o) {
    if (!distinctValues.Contains(o)) {
        distinctValues.Add(o);
    }
});

// Copy original list as a list with duplicates
List<object> duplicateValues = new List<object>(oList);

// Remove the first instance of each distinct value
// leaving all duplicates
distinctValues.ForEach(delegate(object o){
    duplicateValues.Remove(o);
});

// Display distinct values
foreach (object o in distinctValues) {
    MessageBox.Show(o.ToString(), "Distinct");
}

// Display duplicate values
foreach (object o in duplicateValues) { 
    MessageBox.Show(o.ToString(), "Duplicate");
}
using System; using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program 
{ 
static void Main() List list = new List();
Console.WriteLine("number: "); int num = int.Parse(Console.ReadLine());
while (num > 0) { list.Add(num % 10); num /= 10; } list.Reverse();
List distinct = list.Distinct().ToList();
PrintValues(distinct);
static void PrintValues(IEnumerable distinct) 
{
 foreach (object value in distinct) Console.Write("{0}", value); 
  Console.WriteLine();
} 
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top