Remove groups of consecutive numbers from array in Ruby
I have an array:
[1, 2, 3, 6, 8, 9, 10, 23, 34, 35, 36, 45, 50, 51, ...]
I'm trying to remove each group of consecutive numbers so I end up with:
[6, 23, 45, ...]
I am looking for anomalies in serial ids. Does anyone have suggestions?
My initial attempt only checks for the id before each element:
non_consecutive_ids =
ids.each_with_index do |x, i|
unless x == ids[i-1] + 1
non_consecutive_ids << x
end
end
The thing I think I was missing was to also check to see if the next element in the array is 1 more than the current.
ruby
add a comment |
I have an array:
[1, 2, 3, 6, 8, 9, 10, 23, 34, 35, 36, 45, 50, 51, ...]
I'm trying to remove each group of consecutive numbers so I end up with:
[6, 23, 45, ...]
I am looking for anomalies in serial ids. Does anyone have suggestions?
My initial attempt only checks for the id before each element:
non_consecutive_ids =
ids.each_with_index do |x, i|
unless x == ids[i-1] + 1
non_consecutive_ids << x
end
end
The thing I think I was missing was to also check to see if the next element in the array is 1 more than the current.
ruby
1
Possible duplicate of Select consecutive integers from array in Ruby
– anothermh
Nov 20 '18 at 18:24
would it not be3, 6, 10, 23, 36, 45, 51?
– Josh Brody
Nov 20 '18 at 18:24
What have you tried? Where is the work you have done so far in trying to solve this problem on your own? Why don't any of the many similar answered questions on stackoverflow answer the question for you?
– anothermh
Nov 20 '18 at 18:26
@anothermh The answer to the possible duplicate you referenced has what I need, but the question is the opposite of what I was trying to do. It's also flagged as not a very good question. I searched, but most of the questions I found were not doing exactly what I was looking for. Will update my question with the code I first attempted with.
– Ryan Clark
Nov 20 '18 at 18:45
@JoshBrody That could be a resulting array, but not what I'm looking for. I really do want the result to exclude each entire group of consecutive numbers.
– Ryan Clark
Nov 20 '18 at 18:49
add a comment |
I have an array:
[1, 2, 3, 6, 8, 9, 10, 23, 34, 35, 36, 45, 50, 51, ...]
I'm trying to remove each group of consecutive numbers so I end up with:
[6, 23, 45, ...]
I am looking for anomalies in serial ids. Does anyone have suggestions?
My initial attempt only checks for the id before each element:
non_consecutive_ids =
ids.each_with_index do |x, i|
unless x == ids[i-1] + 1
non_consecutive_ids << x
end
end
The thing I think I was missing was to also check to see if the next element in the array is 1 more than the current.
ruby
I have an array:
[1, 2, 3, 6, 8, 9, 10, 23, 34, 35, 36, 45, 50, 51, ...]
I'm trying to remove each group of consecutive numbers so I end up with:
[6, 23, 45, ...]
I am looking for anomalies in serial ids. Does anyone have suggestions?
My initial attempt only checks for the id before each element:
non_consecutive_ids =
ids.each_with_index do |x, i|
unless x == ids[i-1] + 1
non_consecutive_ids << x
end
end
The thing I think I was missing was to also check to see if the next element in the array is 1 more than the current.
ruby
ruby
edited Nov 21 '18 at 3:06


sawa
131k29202301
131k29202301
asked Nov 20 '18 at 18:16
Ryan ClarkRyan Clark
5551725
5551725
1
Possible duplicate of Select consecutive integers from array in Ruby
– anothermh
Nov 20 '18 at 18:24
would it not be3, 6, 10, 23, 36, 45, 51?
– Josh Brody
Nov 20 '18 at 18:24
What have you tried? Where is the work you have done so far in trying to solve this problem on your own? Why don't any of the many similar answered questions on stackoverflow answer the question for you?
– anothermh
Nov 20 '18 at 18:26
@anothermh The answer to the possible duplicate you referenced has what I need, but the question is the opposite of what I was trying to do. It's also flagged as not a very good question. I searched, but most of the questions I found were not doing exactly what I was looking for. Will update my question with the code I first attempted with.
– Ryan Clark
Nov 20 '18 at 18:45
@JoshBrody That could be a resulting array, but not what I'm looking for. I really do want the result to exclude each entire group of consecutive numbers.
– Ryan Clark
Nov 20 '18 at 18:49
add a comment |
1
Possible duplicate of Select consecutive integers from array in Ruby
– anothermh
Nov 20 '18 at 18:24
would it not be3, 6, 10, 23, 36, 45, 51?
– Josh Brody
Nov 20 '18 at 18:24
What have you tried? Where is the work you have done so far in trying to solve this problem on your own? Why don't any of the many similar answered questions on stackoverflow answer the question for you?
– anothermh
Nov 20 '18 at 18:26
@anothermh The answer to the possible duplicate you referenced has what I need, but the question is the opposite of what I was trying to do. It's also flagged as not a very good question. I searched, but most of the questions I found were not doing exactly what I was looking for. Will update my question with the code I first attempted with.
– Ryan Clark
Nov 20 '18 at 18:45
@JoshBrody That could be a resulting array, but not what I'm looking for. I really do want the result to exclude each entire group of consecutive numbers.
– Ryan Clark
Nov 20 '18 at 18:49
1
1
Possible duplicate of Select consecutive integers from array in Ruby
– anothermh
Nov 20 '18 at 18:24
Possible duplicate of Select consecutive integers from array in Ruby
– anothermh
Nov 20 '18 at 18:24
would it not be
3, 6, 10, 23, 36, 45, 51?
– Josh Brody
Nov 20 '18 at 18:24
would it not be
3, 6, 10, 23, 36, 45, 51?
– Josh Brody
Nov 20 '18 at 18:24
What have you tried? Where is the work you have done so far in trying to solve this problem on your own? Why don't any of the many similar answered questions on stackoverflow answer the question for you?
– anothermh
Nov 20 '18 at 18:26
What have you tried? Where is the work you have done so far in trying to solve this problem on your own? Why don't any of the many similar answered questions on stackoverflow answer the question for you?
– anothermh
Nov 20 '18 at 18:26
@anothermh The answer to the possible duplicate you referenced has what I need, but the question is the opposite of what I was trying to do. It's also flagged as not a very good question. I searched, but most of the questions I found were not doing exactly what I was looking for. Will update my question with the code I first attempted with.
– Ryan Clark
Nov 20 '18 at 18:45
@anothermh The answer to the possible duplicate you referenced has what I need, but the question is the opposite of what I was trying to do. It's also flagged as not a very good question. I searched, but most of the questions I found were not doing exactly what I was looking for. Will update my question with the code I first attempted with.
– Ryan Clark
Nov 20 '18 at 18:45
@JoshBrody That could be a resulting array, but not what I'm looking for. I really do want the result to exclude each entire group of consecutive numbers.
– Ryan Clark
Nov 20 '18 at 18:49
@JoshBrody That could be a resulting array, but not what I'm looking for. I really do want the result to exclude each entire group of consecutive numbers.
– Ryan Clark
Nov 20 '18 at 18:49
add a comment |
2 Answers
2
active
oldest
votes
You can use select
and check the surrounding values:
array.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)}
array = [1,3,0]; arr.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)} #=> [0]
. The problem is whenindex #=> 0
,array[index-1] #=> 0
.
– Cary Swoveland
Nov 21 '18 at 3:17
add a comment |
Other option:
array.chunk_while { |i, j| i + 1 == j }.select { |e| e.size == 1 }.flatten
#=> [6, 23, 45]
The good of Enumerable#chunk_while is that it takes two params. The core doc has just an example of a one-by-one increasing subsequence.
1
Nice one. Invariably, one can flip a coin to decide whether to useEnumerable#chunk_while
or Enumerable#slice_when (a.slice_when { |a,b| b != a + 1 }...
).
– Cary Swoveland
Nov 21 '18 at 3:56
@CarySwoveland, yes. Edit. Thanks.
– iGian
Nov 21 '18 at 6:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399120%2fremove-groups-of-consecutive-numbers-from-array-in-ruby%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use select
and check the surrounding values:
array.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)}
array = [1,3,0]; arr.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)} #=> [0]
. The problem is whenindex #=> 0
,array[index-1] #=> 0
.
– Cary Swoveland
Nov 21 '18 at 3:17
add a comment |
You can use select
and check the surrounding values:
array.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)}
array = [1,3,0]; arr.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)} #=> [0]
. The problem is whenindex #=> 0
,array[index-1] #=> 0
.
– Cary Swoveland
Nov 21 '18 at 3:17
add a comment |
You can use select
and check the surrounding values:
array.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)}
You can use select
and check the surrounding values:
array.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)}
answered Nov 20 '18 at 18:25


Sara TibbettsSara Tibbetts
2,06122241
2,06122241
array = [1,3,0]; arr.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)} #=> [0]
. The problem is whenindex #=> 0
,array[index-1] #=> 0
.
– Cary Swoveland
Nov 21 '18 at 3:17
add a comment |
array = [1,3,0]; arr.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)} #=> [0]
. The problem is whenindex #=> 0
,array[index-1] #=> 0
.
– Cary Swoveland
Nov 21 '18 at 3:17
array = [1,3,0]; arr.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)} #=> [0]
. The problem is when index #=> 0
, array[index-1] #=> 0
.– Cary Swoveland
Nov 21 '18 at 3:17
array = [1,3,0]; arr.select.with_index{ |x, index| (array[index-1] != x-1) && (array[index+1] != x+1)} #=> [0]
. The problem is when index #=> 0
, array[index-1] #=> 0
.– Cary Swoveland
Nov 21 '18 at 3:17
add a comment |
Other option:
array.chunk_while { |i, j| i + 1 == j }.select { |e| e.size == 1 }.flatten
#=> [6, 23, 45]
The good of Enumerable#chunk_while is that it takes two params. The core doc has just an example of a one-by-one increasing subsequence.
1
Nice one. Invariably, one can flip a coin to decide whether to useEnumerable#chunk_while
or Enumerable#slice_when (a.slice_when { |a,b| b != a + 1 }...
).
– Cary Swoveland
Nov 21 '18 at 3:56
@CarySwoveland, yes. Edit. Thanks.
– iGian
Nov 21 '18 at 6:12
add a comment |
Other option:
array.chunk_while { |i, j| i + 1 == j }.select { |e| e.size == 1 }.flatten
#=> [6, 23, 45]
The good of Enumerable#chunk_while is that it takes two params. The core doc has just an example of a one-by-one increasing subsequence.
1
Nice one. Invariably, one can flip a coin to decide whether to useEnumerable#chunk_while
or Enumerable#slice_when (a.slice_when { |a,b| b != a + 1 }...
).
– Cary Swoveland
Nov 21 '18 at 3:56
@CarySwoveland, yes. Edit. Thanks.
– iGian
Nov 21 '18 at 6:12
add a comment |
Other option:
array.chunk_while { |i, j| i + 1 == j }.select { |e| e.size == 1 }.flatten
#=> [6, 23, 45]
The good of Enumerable#chunk_while is that it takes two params. The core doc has just an example of a one-by-one increasing subsequence.
Other option:
array.chunk_while { |i, j| i + 1 == j }.select { |e| e.size == 1 }.flatten
#=> [6, 23, 45]
The good of Enumerable#chunk_while is that it takes two params. The core doc has just an example of a one-by-one increasing subsequence.
edited Nov 21 '18 at 6:12
answered Nov 20 '18 at 19:10


iGianiGian
3,8582623
3,8582623
1
Nice one. Invariably, one can flip a coin to decide whether to useEnumerable#chunk_while
or Enumerable#slice_when (a.slice_when { |a,b| b != a + 1 }...
).
– Cary Swoveland
Nov 21 '18 at 3:56
@CarySwoveland, yes. Edit. Thanks.
– iGian
Nov 21 '18 at 6:12
add a comment |
1
Nice one. Invariably, one can flip a coin to decide whether to useEnumerable#chunk_while
or Enumerable#slice_when (a.slice_when { |a,b| b != a + 1 }...
).
– Cary Swoveland
Nov 21 '18 at 3:56
@CarySwoveland, yes. Edit. Thanks.
– iGian
Nov 21 '18 at 6:12
1
1
Nice one. Invariably, one can flip a coin to decide whether to use
Enumerable#chunk_while
or Enumerable#slice_when (a.slice_when { |a,b| b != a + 1 }...
).– Cary Swoveland
Nov 21 '18 at 3:56
Nice one. Invariably, one can flip a coin to decide whether to use
Enumerable#chunk_while
or Enumerable#slice_when (a.slice_when { |a,b| b != a + 1 }...
).– Cary Swoveland
Nov 21 '18 at 3:56
@CarySwoveland, yes. Edit. Thanks.
– iGian
Nov 21 '18 at 6:12
@CarySwoveland, yes. Edit. Thanks.
– iGian
Nov 21 '18 at 6:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399120%2fremove-groups-of-consecutive-numbers-from-array-in-ruby%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of Select consecutive integers from array in Ruby
– anothermh
Nov 20 '18 at 18:24
would it not be
3, 6, 10, 23, 36, 45, 51?
– Josh Brody
Nov 20 '18 at 18:24
What have you tried? Where is the work you have done so far in trying to solve this problem on your own? Why don't any of the many similar answered questions on stackoverflow answer the question for you?
– anothermh
Nov 20 '18 at 18:26
@anothermh The answer to the possible duplicate you referenced has what I need, but the question is the opposite of what I was trying to do. It's also flagged as not a very good question. I searched, but most of the questions I found were not doing exactly what I was looking for. Will update my question with the code I first attempted with.
– Ryan Clark
Nov 20 '18 at 18:45
@JoshBrody That could be a resulting array, but not what I'm looking for. I really do want the result to exclude each entire group of consecutive numbers.
– Ryan Clark
Nov 20 '18 at 18:49