How to group Arrays [duplicate]











up vote
-2
down vote

favorite













This question already has an answer here:




  • Arrays of Int arrays. Storing duplicates in order only

    6 answers




How to group Arrays by a first same elements
for example I have Array like that"



var Array = ["1","1","1","2","2","1","1"]


I want to group the Array like :



groupedArray = [
["1","1","1"],
["2","2"],
["1","1"]
]


Thank you!










share|improve this question













marked as duplicate by Leo Dabus swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    What have you tried so far? What does your existing code look like?
    – ZGski
    Nov 21 at 21:16










  • Iterate over all elements, keeping track of the current "run". When a run is complete add it to groupedArray. Don't forget to add the last run when the main iteration is done.
    – Steve Kuo
    Nov 21 at 21:36

















up vote
-2
down vote

favorite













This question already has an answer here:




  • Arrays of Int arrays. Storing duplicates in order only

    6 answers




How to group Arrays by a first same elements
for example I have Array like that"



var Array = ["1","1","1","2","2","1","1"]


I want to group the Array like :



groupedArray = [
["1","1","1"],
["2","2"],
["1","1"]
]


Thank you!










share|improve this question













marked as duplicate by Leo Dabus swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    What have you tried so far? What does your existing code look like?
    – ZGski
    Nov 21 at 21:16










  • Iterate over all elements, keeping track of the current "run". When a run is complete add it to groupedArray. Don't forget to add the last run when the main iteration is done.
    – Steve Kuo
    Nov 21 at 21:36















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite












This question already has an answer here:




  • Arrays of Int arrays. Storing duplicates in order only

    6 answers




How to group Arrays by a first same elements
for example I have Array like that"



var Array = ["1","1","1","2","2","1","1"]


I want to group the Array like :



groupedArray = [
["1","1","1"],
["2","2"],
["1","1"]
]


Thank you!










share|improve this question














This question already has an answer here:




  • Arrays of Int arrays. Storing duplicates in order only

    6 answers




How to group Arrays by a first same elements
for example I have Array like that"



var Array = ["1","1","1","2","2","1","1"]


I want to group the Array like :



groupedArray = [
["1","1","1"],
["2","2"],
["1","1"]
]


Thank you!





This question already has an answer here:




  • Arrays of Int arrays. Storing duplicates in order only

    6 answers








arrays swift






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 21:10









Faris

1551214




1551214




marked as duplicate by Leo Dabus swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Leo Dabus swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    What have you tried so far? What does your existing code look like?
    – ZGski
    Nov 21 at 21:16










  • Iterate over all elements, keeping track of the current "run". When a run is complete add it to groupedArray. Don't forget to add the last run when the main iteration is done.
    – Steve Kuo
    Nov 21 at 21:36
















  • 2




    What have you tried so far? What does your existing code look like?
    – ZGski
    Nov 21 at 21:16










  • Iterate over all elements, keeping track of the current "run". When a run is complete add it to groupedArray. Don't forget to add the last run when the main iteration is done.
    – Steve Kuo
    Nov 21 at 21:36










2




2




What have you tried so far? What does your existing code look like?
– ZGski
Nov 21 at 21:16




What have you tried so far? What does your existing code look like?
– ZGski
Nov 21 at 21:16












Iterate over all elements, keeping track of the current "run". When a run is complete add it to groupedArray. Don't forget to add the last run when the main iteration is done.
– Steve Kuo
Nov 21 at 21:36






Iterate over all elements, keeping track of the current "run". When a run is complete add it to groupedArray. Don't forget to add the last run when the main iteration is done.
– Steve Kuo
Nov 21 at 21:36














2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You can extend Collection, constrain its Element to Equatable protocol and create a computed property to return the grouped elements using reduce(into:) method. You just need to check if there is a last element on the last collection equal to the current element and append the current element to the last collection if true otherwise append a new collection with it:



extension Collection where Element: Equatable {
var grouped: [[Element]] {
return reduce(into: ) {
$0.last?.last == $1 ?
$0[$0.index(before: $0.endIndex)].append($1) :
$0.append([$1])
}
}
}




let array = ["1","1","1","2","2","1","1"]
let grouped = array.grouped
print(grouped) // "[["1", "1", "1"], ["2", "2"], ["1", "1"]]n"





share|improve this answer






























    up vote
    2
    down vote













    Don't name your variable Array, you are masking the Swift.Array type and will cause untold number of weird bugs. Variable names in Swift should start with a lowercase letter.



    Use prefix(while:) to gather identical elements starting from a specified index. And then you keep advancing that index:



    let array = ["1","1","1","2","2","1","1"]

    var index = 0
    var groupedArray = [[String]]()
    while index < array.count {
    let slice = array[index...].prefix(while: { $0 == array[index] })
    groupedArray.append(Array(slice))
    index = slice.endIndex
    }





    share|improve this answer




























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      You can extend Collection, constrain its Element to Equatable protocol and create a computed property to return the grouped elements using reduce(into:) method. You just need to check if there is a last element on the last collection equal to the current element and append the current element to the last collection if true otherwise append a new collection with it:



      extension Collection where Element: Equatable {
      var grouped: [[Element]] {
      return reduce(into: ) {
      $0.last?.last == $1 ?
      $0[$0.index(before: $0.endIndex)].append($1) :
      $0.append([$1])
      }
      }
      }




      let array = ["1","1","1","2","2","1","1"]
      let grouped = array.grouped
      print(grouped) // "[["1", "1", "1"], ["2", "2"], ["1", "1"]]n"





      share|improve this answer



























        up vote
        2
        down vote



        accepted










        You can extend Collection, constrain its Element to Equatable protocol and create a computed property to return the grouped elements using reduce(into:) method. You just need to check if there is a last element on the last collection equal to the current element and append the current element to the last collection if true otherwise append a new collection with it:



        extension Collection where Element: Equatable {
        var grouped: [[Element]] {
        return reduce(into: ) {
        $0.last?.last == $1 ?
        $0[$0.index(before: $0.endIndex)].append($1) :
        $0.append([$1])
        }
        }
        }




        let array = ["1","1","1","2","2","1","1"]
        let grouped = array.grouped
        print(grouped) // "[["1", "1", "1"], ["2", "2"], ["1", "1"]]n"





        share|improve this answer

























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can extend Collection, constrain its Element to Equatable protocol and create a computed property to return the grouped elements using reduce(into:) method. You just need to check if there is a last element on the last collection equal to the current element and append the current element to the last collection if true otherwise append a new collection with it:



          extension Collection where Element: Equatable {
          var grouped: [[Element]] {
          return reduce(into: ) {
          $0.last?.last == $1 ?
          $0[$0.index(before: $0.endIndex)].append($1) :
          $0.append([$1])
          }
          }
          }




          let array = ["1","1","1","2","2","1","1"]
          let grouped = array.grouped
          print(grouped) // "[["1", "1", "1"], ["2", "2"], ["1", "1"]]n"





          share|improve this answer














          You can extend Collection, constrain its Element to Equatable protocol and create a computed property to return the grouped elements using reduce(into:) method. You just need to check if there is a last element on the last collection equal to the current element and append the current element to the last collection if true otherwise append a new collection with it:



          extension Collection where Element: Equatable {
          var grouped: [[Element]] {
          return reduce(into: ) {
          $0.last?.last == $1 ?
          $0[$0.index(before: $0.endIndex)].append($1) :
          $0.append([$1])
          }
          }
          }




          let array = ["1","1","1","2","2","1","1"]
          let grouped = array.grouped
          print(grouped) // "[["1", "1", "1"], ["2", "2"], ["1", "1"]]n"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 16:47

























          answered Nov 21 at 22:03









          Leo Dabus

          129k30263339




          129k30263339
























              up vote
              2
              down vote













              Don't name your variable Array, you are masking the Swift.Array type and will cause untold number of weird bugs. Variable names in Swift should start with a lowercase letter.



              Use prefix(while:) to gather identical elements starting from a specified index. And then you keep advancing that index:



              let array = ["1","1","1","2","2","1","1"]

              var index = 0
              var groupedArray = [[String]]()
              while index < array.count {
              let slice = array[index...].prefix(while: { $0 == array[index] })
              groupedArray.append(Array(slice))
              index = slice.endIndex
              }





              share|improve this answer

























                up vote
                2
                down vote













                Don't name your variable Array, you are masking the Swift.Array type and will cause untold number of weird bugs. Variable names in Swift should start with a lowercase letter.



                Use prefix(while:) to gather identical elements starting from a specified index. And then you keep advancing that index:



                let array = ["1","1","1","2","2","1","1"]

                var index = 0
                var groupedArray = [[String]]()
                while index < array.count {
                let slice = array[index...].prefix(while: { $0 == array[index] })
                groupedArray.append(Array(slice))
                index = slice.endIndex
                }





                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Don't name your variable Array, you are masking the Swift.Array type and will cause untold number of weird bugs. Variable names in Swift should start with a lowercase letter.



                  Use prefix(while:) to gather identical elements starting from a specified index. And then you keep advancing that index:



                  let array = ["1","1","1","2","2","1","1"]

                  var index = 0
                  var groupedArray = [[String]]()
                  while index < array.count {
                  let slice = array[index...].prefix(while: { $0 == array[index] })
                  groupedArray.append(Array(slice))
                  index = slice.endIndex
                  }





                  share|improve this answer












                  Don't name your variable Array, you are masking the Swift.Array type and will cause untold number of weird bugs. Variable names in Swift should start with a lowercase letter.



                  Use prefix(while:) to gather identical elements starting from a specified index. And then you keep advancing that index:



                  let array = ["1","1","1","2","2","1","1"]

                  var index = 0
                  var groupedArray = [[String]]()
                  while index < array.count {
                  let slice = array[index...].prefix(while: { $0 == array[index] })
                  groupedArray.append(Array(slice))
                  index = slice.endIndex
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 22:01









                  Code Different

                  46k773105




                  46k773105















                      Popular posts from this blog

                      Berounka

                      Fiat S.p.A.

                      Type 'String' is not a subtype of type 'int' of 'index'