I need help on an algorithm. I have randomly generated numbers with 6 digits. Like;
123654
109431
There are approximately 1 million of them saved in a file line by line. I have to filter them according to the rule I try to describe below.
Take a number, compare it to all others digit by digit. If a number comes up with a digit with a value of bigger by one to the compared number, then delete it. Let me show it by using numbers.
Our number is: 123456
Increase the first digit with 1, so the number becomes: 223456. Delete all the 223456s from the file.
Increase the second digit by 1, the number becomes: 133456. Delete all 133456s from the file, and so on…
I can do it just as I describe but I need it to be “FAST”.
So can anyone help me on this?
Thanks.
This algorithm will keep a lot of numbers around in memory, but it will process the file one number at a time so you don’t actually need to read it all in at once. You only need to supply an
IEnumerable<int>for it to operate on.You can use this method to create an
IEnumerable<int>from the file: