Delete all occurrences of value in Swift Array [duplicate] - arrays

This question already has answers here:
Remove matched item from array of objects?
(5 answers)
Closed 7 years ago.
So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?

The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}

Related

Python numpy: Selecting array entries based on input array [duplicate]

This question already has answers here:
Getting the indices of several elements in a NumPy array at once
(5 answers)
Closed 2 years ago.
Assume I have an array:
a = np.array([1,2,3,4,5])
Now I want to find the indices of elements in this array corresponding to the values given by another array input:
input = np.array([2,4,5])
The expected result should be:
result = [1,3,4]
A boolean mask, which is true for element indices 1,3,4 would also be fine.
I do not want to use looping to solve this. I assume that a possible solution has to do with the numpy where() function, but using this one, I am only able to compare the entries of array a with one element of array input at a time. Because the length of input might differ, I cannot really use this approach. Do you have any other ideas?
Thanks in advance.
np.where(np.in1d(a, inp))[0]
or:
np.isin(a, inp).nonzero()[0]
or as suggested here:
sorter = np.argsort(a)
sorter[np.searchsorted(a, inp, sorter=sorter)]
output:
[1 3 4]
np.where(np.in1d(a, inp))[0] np.where(np.in1d(a, inp))[0]

Check item contains in model object array in swift 3 [duplicate]

This question already has answers here:
how can I check if a structure is in the array of structures based on its field in Swift3?
(3 answers)
Get element from array of dictionaries according to key
(1 answer)
Closed 5 years ago.
I have a model object called user.
how to find my user.id from all users collection.
I need something like this UsersCollection.contains(user.id)
and returns Bool value.
Here you go.
let contains = UsersCollection.contains{ $0.id == 0 }
Edit:
let object = UsersCollection.first{ $0.id == 0 } // object is optional here.

How to create array and display required array value [duplicate]

This question already has answers here:
How does one declare an array in VBScript?
(2 answers)
Closed 5 years ago.
how to create array and display required array value. For example:
Arr[0]="anand" Arr[1]="bala".print Arr optin:2, Bala .
i need the required array output
Queat seems a bit unclear.
May be below piece of code can be relevent to your Question.
'Initializing & Filling up array
Dim testArr(3)
testArr(0)="First"
testArr(1)="Second"
testArr(2)="Third"
Dim Iterator,userInput
'If you Need ro validate the User input you need to use Err Object.
'Like if it's number or not/Is teh User Provided value is positive or not/ etc. etc. which i'm not doing now)
userInput=Cint(inputbox("Please enter a Positive number between 1-" & Ubound(testArr)))
If userInput<=Ubound(testArr) Then
MsgBox(testArr(userInput-1))
Else
MsgBox("Wrong Value Provided. Not Applicable Array Index Number !!!")
End If

Removing multiple elements from array by index in Ruby [duplicate]

This question already has answers here:
Delete contents of array based on a set of indexes
(4 answers)
Closed 5 years ago.
I have an array. For example,
x = [1,2,3,4,5]
I know the command
x.delete_at(i)
will delete the element at index i from the array. But from what I've read, it can only handle one argument.
Let's say I have a variable that stores the indexes I wish to remove from x. For example,
y = [0,2,3]
My question: Is it possible to remove multiple elements from an array using another array that stores in the indexes you wish to delete at?
In essence, something like
x.delete_at(y)
Thanks! :)
You can use reject with with_index:
x.reject.with_index { |e, i| y.include? i } #=> [2, 5]

How do I check if an array index is out of range SWIFT [duplicate]

This question already has answers here:
Swift Array - Check if an index exists
(12 answers)
Closed 7 years ago.
I want to be able to check when my array index goes out of range. The array elements are all strings, so I tried to do something like this but it doesn't work:
if questions[3] != nil {
}
Can someone just show me how to check?
Before indexing into the array, you need to check:
The intended index is not below 0
array's count is above the intended index
let intendedIndex: Int = 3
if (intendedIndex >= 0 && questions.count > intendedIndex) {
// This line will not throw index out of range:
let question3 = questions[intendedIndex]
}

Resources