Question about comparing comma separated lists

  • Thread starter Thread starter Brian Simmons
  • Start date Start date
B

Brian Simmons

Hi All,

Thought of several ways to accomplish what I want to do, but I'm asking to
see if perhaps there a preferred method or super efficient way.

Say I've got 2 strings which are comma separated lists:
"1,2,3,4,5"
"5,4,3,2,1"

I want to make sure the 2 lists are equal, i.e. they contain the same data
elements (not necessarily in the same order).
Such that:
"1,2,3,4,5" equals "5,4,3,2,1"
"1,2,3,4,5" equals "5,3,4,1,2"
"1,2,3,4,5" does not equal "5,4,3,2,1,0"

Any good suggestions?

Thanks,
Brian
 
Thought of several ways to accomplish what I want to do, but I'm asking to
see if perhaps there a preferred method or super efficient way.

Say I've got 2 strings which are comma separated lists:
"1,2,3,4,5"
"5,4,3,2,1"

I want to make sure the 2 lists are equal, i.e. they contain the same data
elements (not necessarily in the same order).
Such that:
"1,2,3,4,5" equals "5,4,3,2,1"
"1,2,3,4,5" equals "5,3,4,1,2"
"1,2,3,4,5" does not equal "5,4,3,2,1,0"

Any good suggestions?

Simple way to do it:

1) Parse the two strings into two lists
2) Compare length of lists (early negative)
3) Sort the lists
4) Compare each element of the lists

Jon
 
Just thinking aloud... how about something like using a dictionary to
tick off items as you find them? You might (or might not) want to
Trim() the keys...

Other solutions will be valid ;-p

static bool IsMatch(string lhs, string rhs) {
Dictionary<string, int> lookup = new Dictionary<string,
int>();
int count;
// build a lookup of keys and their respective counts
foreach(string key in lhs.Split(',')) {
if (lookup.TryGetValue(key, out count)) {
lookup[key] = count + 1;
} else {
lookup.Add(key, 1);
}
}
// enumerate the comparison string, reducing counts as found
(comsumption)
foreach (string key in rhs.Split(',')) {
if (!lookup.TryGetValue(key, out count) || count <= 0)
return false; // not there at all, or already used
lookup[key] = count - 1; // reduce count
}
// check that everything is fully accounted for
foreach (int val in lookup.Values) {
if (val != 0) return false; // not comsumed entirely
}
return true;
}
 
I'd parse the first list, adding to a StringDictionary. This is a hash table
struture so indexing existing content is super fast. Then parse the second
list, doing a check on the StringDictionary object to see if it exists... If
it doesn't exit your check with a "failed", otherwise you know they're the
same...
 
Brian,

In .NET 3.5 (in beta now), you would use the HashSet class, which has
methods to allow you to determine if two sets are the same (you would have
to parse the comma delimited lists apart first).

You can still do easily do this in .NET 3.0. I would start by
converting the comma delimited lists into two arrays, like so:

// The string.
string s1 = "1,2,3,4,5";

// Parse and convert to a list.
int[] sortedList1 = Array.Sort<int>(Array.ConvertAll<string, int>(
s1.Split(new char[]{','}),
delegate(string val) { return Int32.Parse(val) }));

Once you have the two lists, you can check to see if the number of
elements are the same. If they are not, you know they are not equal. If
they are, then you have to cycle through each element in both lists,
comparing to see if they are the same.

Now, this assumes that there are not repeating elements in the list. If
there are, you will have to work to remove those elements before you perform
the comparison (assuming that is a condition).
 
Brian Simmons said:
Hi All,

Thought of several ways to accomplish what I want to do, but I'm asking to
see if perhaps there a preferred method or super efficient way.

Say I've got 2 strings which are comma separated lists:
"1,2,3,4,5"
"5,4,3,2,1"

I want to make sure the 2 lists are equal, i.e. they contain the same data
elements (not necessarily in the same order).
Such that:
"1,2,3,4,5" equals "5,4,3,2,1"
"1,2,3,4,5" equals "5,3,4,1,2"
"1,2,3,4,5" does not equal "5,4,3,2,1,0"

The only way I see is, to split the strings sort the resulting arrays, and
then compare the arrays element by element.

Christof
 
I did some metrics... no benefit in this approach (over a simple
sort). Don't do it; keep it simple and use a sort check.

I was just off one one - but at least I have enough sense to check ;-p
An interesting diversion, none-the-less.

Marc
 
Now, this assumes that there are not repeating elements in the list.
If there are, you will have to work to remove those elements before
you perform the comparison (assuming that is a condition).

Why would you have to remove the repeated elements? You'd only have
todo that if you explicitly wanted 1,1,1,1,2 to be equal to 1,2 - which
when they're viewed as *lists* they're not.
 
Well, the OP said list, but what he implied was set, as lists an order
to them, but he wanted the equality semantics of a set, ("1,2..." and
"...2,1"), so it can't be said for sure whether or not there is going to be
repeated data or not, since the OP isn't clear one way or another on the
matter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top