Remove groups of consecutive numbers from array in Ruby












2















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.










share|improve this question




















  • 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
















2















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.










share|improve this question




















  • 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














2












2








2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












2 Answers
2






active

oldest

votes


















2














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)}





share|improve this answer
























  • 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





















6














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.






share|improve this answer





















  • 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













  • @CarySwoveland, yes. Edit. Thanks.

    – iGian
    Nov 21 '18 at 6:12











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
});


}
});














draft saved

draft discarded


















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









2














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)}





share|improve this answer
























  • 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


















2














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)}





share|improve this answer
























  • 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
















2












2








2







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)}





share|improve this answer













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)}






share|improve this answer












share|improve this answer



share|improve this answer










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 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



















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















6














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.






share|improve this answer





















  • 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













  • @CarySwoveland, yes. Edit. Thanks.

    – iGian
    Nov 21 '18 at 6:12
















6














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.






share|improve this answer





















  • 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













  • @CarySwoveland, yes. Edit. Thanks.

    – iGian
    Nov 21 '18 at 6:12














6












6








6







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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














  • 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













  • @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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory