How to group Arrays [duplicate]
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!
arrays swift
marked as duplicate by Leo Dabus
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 '18 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.
add a comment |
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!
arrays swift
marked as duplicate by Leo Dabus
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 '18 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 '18 at 21:16
Iterate over all elements, keeping track of the current "run". When a run is complete add it togroupedArray
. Don't forget to add the last run when the main iteration is done.
– Steve Kuo
Nov 21 '18 at 21:36
add a comment |
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!
arrays swift
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
arrays swift
asked Nov 21 '18 at 21:10
FarisFaris
1751214
1751214
marked as duplicate by Leo Dabus
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 '18 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
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 '18 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 '18 at 21:16
Iterate over all elements, keeping track of the current "run". When a run is complete add it togroupedArray
. Don't forget to add the last run when the main iteration is done.
– Steve Kuo
Nov 21 '18 at 21:36
add a comment |
2
What have you tried so far? What does your existing code look like?
– ZGski
Nov 21 '18 at 21:16
Iterate over all elements, keeping track of the current "run". When a run is complete add it togroupedArray
. Don't forget to add the last run when the main iteration is done.
– Steve Kuo
Nov 21 '18 at 21:36
2
2
What have you tried so far? What does your existing code look like?
– ZGski
Nov 21 '18 at 21:16
What have you tried so far? What does your existing code look like?
– ZGski
Nov 21 '18 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 '18 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 '18 at 21:36
add a comment |
2 Answers
2
active
oldest
votes
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"
add a comment |
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
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"
add a comment |
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"
add a comment |
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"
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"
edited Nov 22 '18 at 16:47
answered Nov 21 '18 at 22:03
Leo DabusLeo Dabus
130k30266341
130k30266341
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Nov 21 '18 at 22:01
Code DifferentCode Different
46.8k774107
46.8k774107
add a comment |
add a comment |
2
What have you tried so far? What does your existing code look like?
– ZGski
Nov 21 '18 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 '18 at 21:36