Finding difference between numbers

Hi,

I have a list of numbers like below
List(111) { 0, 1, 2, 3, 5, 7, 8, 9, 10, 11, 13, 14, 16, 17, 18, 19, 20, 22, 23, 24, 25, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 46, 50, 72, 77, 81, 104, 106, 112, 114, 115, 116, 117, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 162,
163, 164, 165, 166, 167, 168, 169, 170, 173, 177, 181, 182, 183, 188, 195, 204, 207, 210, 212, 213, 214, 215, 217 }

my requirement is to find difference with the next number and add that number to a new list,
Like for example
0-1=1
1-2=1
2-3=1
3-5=2

Any help would be appreciated

Hi @Stanleycollege

Welcome to the UiPath Community

This can be done as follows:

  1. Create the required list of (int32) and initialize it.

image

New List(Of Int32) From { 0, 1, 2, 3, 5, 7, 8, 9, 10, 11, 13, 14, 16, 17, 18, 19, 20, 22, 23, 24, 25, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 46, 50, 72, 77, 81, 104, 106, 112, 114, 115, 116, 117, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 162,
163, 164, 165, 166, 167, 168, 169, 170, 173, 177, 181, 182, 183, 188, 195, 204, 207, 210, 212, 213, 214, 215, 217 }
  1. Create a List (Of int32) variable (diffList) to store the result

image

Enumerable.Range(0, Cint(numbers.Count)-1).Select(function(i) math.Abs(numbers(i)-numbers(i+1))).ToList
  1. You can check the output by printing the values of diffList

Hope it helps!

Attachment

diff between numbers.xaml (5.2 KB)

Hi,

Another solution:

result = yourList.Skip(1).Select(Function(x,i) x-yourList(i)).ToList

Regards,

2 Likes

@Yoichi Wow!

1 Like

both the solutions working for my requirement. Thanks.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.