2

I'm new in IOS programming. I have a question, how to get all value in array except x value. Let say i have array like below :

let array : [Any] = [1,2,3,4,5,6,7,8,9,0,11,22,33,44,55,66,77,200]

how to print all value except 1 and 2.

I have read this , its using filter and i try it with playground but i still not have the right value. Any answer will helpfull for me. Thanks in advance .

2
  • 2
    Show your sample code with filter that does not work Commented Apr 10, 2017 at 9:15
  • 1
    Your array is not [Any], it's [Int]. Do not annotate types the compiler can infer. In many cases like this one you make it worse. Commented Apr 10, 2017 at 9:18

3 Answers 3

7

I don't know why you have defined the array as [Any] so I just removed that and the array is:-

let array = [1,2,3,4,5,6,7,8,9,0,11,22,33,44,55,66,77,200]

Next you can use filter as follows:-

let filtered = array.filter { (element) -> Bool in return element != 1 && element != 2 }

You can test this out in the playground, it will print all values except 1 & 2

You can also use some syntactical sugar for filter as follows:-

array.filter({ return $0 != 1 && $0 != 2 })

And since the closure is a trailing argument, you can also separate it from the arguments as follows:-

array.filter { return $0 != 1 && $0 != 2 }

Another way to do this would be

let filterTheseOut = [1,2]

let anotherWay = array.filter { !filterTheseOut.contains($0) }

So here you can basically add all the elements to be filtered out in a separate array

Sign up to request clarification or add additional context in comments.

5 Comments

thanks, i have try if using loop for before and its work for me. but now i want to know if using filter. your answer is better than other
i have a question, what the meaning $0 ?
if a closure takes argument(s) but you do not wish to name those arguments, you can refer to them as $0, $1 etc.
I would also appreciate if you could accept the answer
ohh i see. 5 minutes for checked. Thanks i understand now
1

You can do it like this

let array : [Int] = [1,2,3,4,5,6,7,8,9,0,11,22,33,44,55,66,77,200]

print(array.filter { $0 != 1 && $0 != 2 } )

or if you will have more than 1 or 2 values you can put them into array

let array : [Int] = [1,2,3,4,5,6,7,8,9,0,11,22,33,44,55,66,77,200]
let unwantedValues = [1,2]
print(array.filter { unwantedValues.contains($0) == false } )

Next time please paste your code, it will be easier to tell you what you're doing wrong, then giving you ready solution.

1 Comment

thanks, i think my question is very clear to understand. But next time i will. Thanks again
0

No need for filter use this:

for i in array {
 if i != 1 && i != 2 {
 print i
 }
}

// This will print all values except 1 and 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.