With nested loops:
string[] strs = new string[] { "0111110", "1000001" };
char[] main = strs[0].ToCharArray();
for(int i = 0; i < 7; i++)
{
if(main[i] == '1')
continue;
for(int j = 1; j < strs.Length; j++)
{
if(strs[j][i] == '1')
{
main[i] = '1';
break;
}
}
}
string result = new string(main);
It's more wordy than kristoffer's bitwise or but it should be pretty quick if you're merging a large number of strings, as it gives up as soon as it makes a 1 in a particular position. The bitwise or approach is a bit more naive and would incur more unnecessary conversions. Which one is more performant probably depends on the particulars of your situation. The bitwise approach may also have some padding/length issues and is limited to 8 digits if using byte; you only have 7 days but someone might upgrade it to do full months one day. This approach doesn't have a limit in that regard
An "N number of strings" bitwise approach:
byte r = 0;
for(int i = 0; i < strs.Length && r != 127; i++)
r |= Convert.ToByte(strs[i], 2);
string res = Convert.ToString(t, 2).PadLeft(7, '0');
We can stop early if we hit 127 as this is indicative of reaching 1111111. Leading zeroes are lost and must be restored with PadLeft