I have a list containing few elements like this: [1, 1, 2, 2, 3, 3]. I want to combine these numbers into one number without summing them so I want the final number to be: 112233; Is there a way to do it
CodePudding user response:
you can use reduce method from list
List<int> list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
String s = "";
for(int i in list) {
s = i.toString();
}
int result = int.parse(s); //result = 12345678910
result maximum value is 2^63-1, otherwise you will get an overflow!!!
CodePudding user response:
This can be easily done with Iterable.fold
or Iterable.reduce
. In your case, since the element types and the result type are the same, you can use reduce
:
void main() {
var digits = [1, 1, 2, 2, 3, 3];
var combined =
digits.reduce((resultSoFar, current) => resultSoFar * 10 current);
print(combined); // Prints: 112233
}