1

I have an array of strings. And also I have variable that randomly chooses one string from it. The problem is – it can repeat values and I don't want that. Every value should be displayed only once. So how would I do that? I know that sets is more appropriate in this case than arrays, but it's more complicated for me

var questions = ["red", "blue", "green", "square", "tasty"]

let randomFact = Int(arc4random_uniform(UInt32(questions.count)))    

For the purpose of my app it should display new value when button is touched. I tried to remove value from array to avoid it's repeating with

questions.removeAtIndex(Int(questions[randomFact])!)

But it crashes.

So how to display every value randomly but only once?

2 Answers 2

1

I like your approach of removing the element after it's chosen. You just have a small issue with your code:

let fact = questions[randomFact]
questions.removeAtIndex(randomFact)
Sign up to request clarification or add additional context in comments.

2 Comments

Can you show me how to roughly do that? I tried to remove it with code above, and it crashed on me. What do you mean with current size? How many items there are left?
@Burundanga I have modified my answer after reading the whole of your question and seeing that you have already considered this approach.
1

If you are performing the removal of the element while you are iterating over the length of the array, this may cause your app to crash (unless you use a removal-safe loop).

Please refer to this SO question for more information on removal-safe iterations.

EDIT 01: Loop solution

The following code works for me, does it accomplish what you are seeking?

var questions = ["red", "blue", "green", "square", "tasty"]

repeat
{
   let randomFact = Int(arc4random_uniform(UInt32(questions.count)))
   print( questions[randomFact] )
   questions.removeAtIndex(randomFact)
}while( questions.count > 0 );

This is the output:

square
tasty
green
red
blue

EDIT 02: General solution

In general, you can remove an element from an array like this:

let randomFact = Int(arc4random_uniform(UInt32(questions.count)))
questions.removeAtIndex(randomFact)

but remember, if you are somehow dependent on the initial size of the array in your code, it will cause your app to crash.

8 Comments

You can see in the question that the OP is not iterating over the array. He generates a random index and uses that to access/remove the element.
The question states that @Burnundanga want to show every value in the array only once, picked randomly. I think that using a loop is inevitable, even if the loop itself is not shown in the code.
No, that's an assumption on your part. He does not state that he uses iteration, only that each string element must be displayed only once. If he's "displaying" then it's more likely each element is chosen slowly allowing the value to be seen. That's not something that implies iteration.
Oh, so for "displaying the value" you are happy that print() is enough? How does the user see that then?
The display part of the App is off topic in this question ("How to access every value of array only once"), @Burnundanga asked about accessing the value of an array only once. My answer exclusevely addresses that. This discussion is going nowhere.
|

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.