Swift 3: Remove one character from set [duplicate] - swift

This question already has answers here:
Removing duplicate elements from an array in Swift
(49 answers)
Closed 5 years ago.
Say I have a set [A, B, B, C, D], how would I remove just the first B?
If charToDelete = B and I do this:
SlidingWin.remove(charToDelete)
won't it remove all of the B characters?

In Swift lingo, what you have is an array (not a set). If you want to remove the first "B" from an array, you can do this:
if let index = array.index(of:"B")
{ array.remove(at:index) }
[EDIT] example of a functional approach for anagrams:
let set1 = "cabb"
let set2 = "cbabeijbbacbkiie"
let anagrams = zip(set2.indices,set2.indices.dropFirst(set1.count-1))
.map{set2[$0...$1]}
.filter{$0.sorted() == set1.sorted()}

Related

Why is x? = y valid syntax in Swift? [duplicate]

This question already has answers here:
Swift optionals - Why does var a:Int? a? = 4 return nil
(2 answers)
Closed 3 years ago.
I am surprised that this compiles
var x: Int? = 3
x? = 5
This seems to do the same thing as x = 5, but it doesn't make sense to me that this would be allowed at all. Would it ever behave differently (like if x was a different type, or if it were a property)?
When you make x optional
x? = 5
if x originally is nil then the line won't run , otherwise it'll act as x = 5

How to create an array by multiplying another array in a functional way? [duplicate]

This question already has answers here:
Repeating array in Swift
(4 answers)
Closed 5 years ago.
I'd like to create an array that contains elements from another array multiplied by some Int value.
Example:
the following code
let arr = [1,2,3]
let multiplier = 3
print(function(arr, multiplier))
should return
[1,2,3,1,2,3,1,2,3]
I know how to make it using nested for loops, but I'm looking for some nifty functional way. I was thinking about map() function, but it iterates over each element of a given array, which is not my use case I suppose.
Main idea:
Create array of arrays,
flatMap to one-dimensional array.
Example:
let arr = [1, 2, 3]
let multiplayer = 3
print(Array(repeating: arr, count: multiplayer).flatMap({ $0 }))

Get Distinct Members of an Array by property [duplicate]

This question already has answers here:
Removing Duplicates From Array of Custom Objects Swift
(4 answers)
Remove objects with duplicate properties from Swift array
(16 answers)
Closed 5 years ago.
I have an array
Contract object contains:
String:id
String:value
Array is:
contract1 = Contract.new()
contract1.id = 2
contract1.value = "Apple"
contract2 = Contract.new()
contract2.id = 2
contract2.value = "Pen"
contract3 = Contract.new()
contract3.id = 1
contract3.value = "Pineapple"
array = [Contract1, Contract2, Contract3]
I would to find out the list of contracts whose IDs are distinct.
I want to have a solution that doesn't make me change the implementation of my object (overriding the isEqual method etc) since I will be using it for more than one object through out my code.
Desired result:
[contract1, contract3] or [contract2, contract3]
Ideally, an extension with additionally a method to only return the values that are being made distinct:
Desired result: [2, 1]
I tried a couple of approaches from similar questions but either the answers are outdated or doesn't fit my need.

How to compare two vectors element by element? [duplicate]

This question already has answers here:
How do I compare all elements of two arrays?
(3 answers)
Closed 6 years ago.
I have two matrices: A=[1,2,3,4,5] and B=[1,2,3,4,6]. I need to compare elements of those matrix, and as a result I need to have binary matrix
Result=[1,1,1,1,0], that means if A(i)==B(i) then Result(i)=1 else Result(i)=0.
I have tried with:
if (isequal (A,B))
Result=1
else
Result=0
end
I have tried: Result=sign(A,B)
I hope that you could help me please?
A = [1,2,3,4,5]
B = [1,2,3,4,6]
Result = A == B

Remove specific integers from matrix [duplicate]

This question already has answers here:
Exclude elements from array [duplicate]
(3 answers)
Closed 6 years ago.
I have a matrix <1x1000> containing integers. It contain the value 150 a couple of times and I want to remove that value completely. Any ideas how to?
Help is much appreciated!
If you want to remove all elements that equal 150 then
M = M(M ~= 150)
If you want to remove all elements belonging to a list of undesired numbers then
list = [150, 230, 420]
M = M(~ismember(M, list))
Same but different expression
M(M==150)=[];
list = [150,230,420];
M(ismember(M,list))=[];
When you type A(index)=[], it delete A(index). For example,
A = [1,2,3];
A(2) = [];
Then
A = [1,3]