Hi,I have a list of numbers which is not sortedand contains duplicate values. I need to get unique value and store it in firstorderlist.My code is not extracting unique values properly.Can anyone help me regarding this.
public static void FindDuplicates(List<Point> actualList)
{
List<Point> firstorderfilterd = new List<Point>();
for (int j = 0; j < actualList.Count; j++)
{
for (int k = j + 1; k < actualList.Count; k++)
{
if (actualList[j] == actualList[k])
{
MessageBox.Show("value" + actualList[j]);
firstorderfilterd.Add(actualList[j]);
}
}
}
MessageBox.Show("firstordefilterednumber" + firstorderfilterd.Count);
}
3 replies to this topic
#1
Posted 15 July 2010 - 02:04 AM
|
|
|
#2
Posted 15 July 2010 - 02:39 AM
use the List.contains(Object) method to see if the ordered list allready contains the value, if it does: don't add it again. If it does not, add it.
for (int i = 0; i < actualList.Count; i++)
{
if ( ! firstorderfilterd.Contains( actualList[i] ) )
{
firstorderfilterd.Add(actualList[i]);
}
}
firstorderfiltered.Sort( );
#3
Posted 15 July 2010 - 03:38 AM
Here is the solution which runs once through the original list and produces purged list on the output.
In this solution, duplicates are purged by first sorting the array (which requires custom comparison function for points). Once done, you just iterate through the list and, each time you notice a new point, you just push it to the output. That's because points that are equal are, after sorting, coming in consecutive positions in the list, hence just pick the first one and discard all the others.
using System;
using System.Drawing;
using System.Collections.Generic;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
List<Point> lst = null;
List<Point> reduced = null;
CreateRandomPoints(out lst, 5000, 0, 100, 0, 100);
ReduceList(lst, out reduced);
Console.WriteLine("Original list: {0} items", lst.Count);
Console.WriteLine("Reduced list: {0} items", reduced.Count);
}
static void CreateRandomPoints(out List<Point> lst, int count, int minX, int maxX, int minY, int maxY)
{
Random rnd = new Random();
int spanX = maxX - minX + 1;
int spanY = maxY - minY + 1;
lst = new List<Point>();
for (int i = 0; i < count; i++)
{
int x = rnd.Next(spanX) + minX;
int y = rnd.Next(spanY) + minY;
lst.Add(new Point(x, y));
}
}
static void ReduceList(List<Point> lst, out List<Point> reduced)
{
reduced = new List<Point>();
// First sort original list:
lst.Sort(new Comparison<Point>(Comparison));
// Then iterate through list and, each time new point occurs, add it to output
Point lastAddedPoint = new Point();
foreach (Point p in lst)
{
bool isNew = false;
if (reduced.Count == 0) // This is the first point so it certainly goes in
isNew = true;
else if (p.X != lastAddedPoint.X || p.Y != lastAddedPoint.Y)
isNew = true;
if (isNew)
{ // Add new point to the reduced list and remember it for further comparison
reduced.Add(p);
lastAddedPoint = p;
}
}
}
static int Comparison(Point p1, Point p2)
{
int result = 0;
if (p1.X < p2.X)
result = -1;
else if (p1.X > p2.X)
result = 1;
else if (p1.Y < p2.Y) // note that beyond this point p1.X == p2.X is true!
result = -1;
else if (p1.Y > p2.Y)
result = 1;
else
result = 0;
return result;
}
}
}
In this solution, duplicates are purged by first sorting the array (which requires custom comparison function for points). Once done, you just iterate through the list and, each time you notice a new point, you just push it to the output. That's because points that are equal are, after sorting, coming in consecutive positions in the list, hence just pick the first one and discard all the others.
#4
Posted 15 July 2010 - 04:15 AM
Thanks :)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









