<p>I'm reading lines of text that can come in any order. The problem is that the output can actually be indentical to the previous output. How can I detect this, without sorting the output first?</p>

<p>Is there some kind of hash function that can take identical input, but in any order, and still produce the same result?</p>

[{'answer_id': 64236, 'author': 'Nicholas', 'author_id': 8054, 'author_profile': 'https://Stackoverflow.com/users/8054', 'pm_score': 0, 'selected': False, 'text': '<p>So you have input like</p>\n\n<pre><code>A B C D\nD E F G\nC B A D\n</code></pre>\n\n<p>and you need to detect that the first and third lines are identical?</p>\n'}, {'answer_id': 64262, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If you want to find out if two files contain the same set of lines, but in a different order, you can use a regular hash function on each line individually, then combine them with a function where ordering doesn't matter, like addition.</p>\n"}, {'answer_id': 64265, 'author': 'HD.', 'author_id': 6525, 'author_profile': 'https://Stackoverflow.com/users/6525', 'pm_score': 0, 'selected': False, 'text': "<p>If the lines are fairly long, you could just keep a list of the hashes of each line -- sort those and compare with previous outputs.</p>\n\n<p>If you don't need a 100% fool-proof solution, you could store the hash of each line in a Bloom filter (look it up on Wikipedia) and compare the Bloom filters at the end of processing.  This can give you false positives (i.e. you think you have the same output but it isn't really the same) but you can tweak the error rate by adjusting the size of the Bloom filter...</p>\n"}, {'answer_id': 64285, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If you add up the ASCII values of each character, you'd get the same result regardless of order.</p>\n\n<p>(This may be a bit too simplified, but perhaps it sparks an idea for you.\nSee Programming Pearls, section 2.8, for an interesting back story.)</p>\n"}, {'answer_id': 64446, 'author': 'SqlRyan', 'author_id': 8114, 'author_profile': 'https://Stackoverflow.com/users/8114', 'pm_score': 3, 'selected': True, 'text': "<p>The easiest way would seem to be to hash each line on the way in, storing the hash and the original data, and then compare each new hash with your collection of existing hashes. If you get a positive, you could compare the actual data, to make sure it's not a false positive - though this would be extremely rare, you could go with a quicker hash algorithm, like MD5 or CRC (instead of something like SHA, which is slower but less likely to collide), just so it's quick, and then compare the actual data when you get a hit.</p>\n"}, {'answer_id': 66238, 'author': 'Alan Krueger', 'author_id': 7708, 'author_profile': 'https://Stackoverflow.com/users/7708', 'pm_score': 0, 'selected': False, 'text': "<p>Any of the hash-based methods may produce bad results because more than one string can produce the same hash.  (It's not likely, but it's possible.)  This is particularly true of the suggestion to add the hashes, since you would essentially be taking a <em>particularly bad</em> hash of the hash values.</p>\n\n<p>A hash method should only be attempted if it's not critical that you miss a change or spot a change where none exists.</p>\n\n<p>The most accurate way would be to keep a Map using the line strings as key and storing the count of each as the value.  (If each string can only appear once, you don't need the count.)  Compute this for the expected set of lines.  Duplicate this collection to examine the incoming lines, reducing the count for each line as you see it.</p>\n\n<ul>\n<li>If you encounter a line with a zero count (or no map entry at all), you've seen a line you didn't expect.</li>\n<li>If you end this with non-zero entries remaining in the Map, you didn't see something you expected.</li>\n</ul>\n"}, {'answer_id': 67573, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Well the problem specification is a bit limited.</p>\n\n<p>As I understand it you wish to see if several strings contain the same elements regardless of order.</p>\n\n<p>For example:</p>\n\n<pre><code>A B C\nC B A\n</code></pre>\n\n<p>are the same.</p>\n\n<p>The way to do this is to create a set of the values then compare the sets. To create a set do:</p>\n\n<pre><code>HashSet set = new HashSet();\nforeach (item : string) {\n   set.add(item);\n}\n</code></pre>\n\n<p>Then just compare the contents of the sets by running through one of the sets and comparing it w/others. The execution time will be <code>O(N)</code> instead of <code>O(NlogN)</code> for the sorting example.</p>\n'}]
['https://Stackoverflow.com/questions/64209', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8406/']