I need to compare a number with an array, if the number is inside the array return the index and delete it from the list. What would be the fasted java method to do this?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why don’t you try a
java.util.LinkedList? With them, you can remove elements in O(1) time.It seems that to search for the number, you’ll be looping through the list. That’ll be O(n) time.
Otherwise maybe you can sort the array, using something like Quicksort, and then do a Binary Search for the value and get the index. Then you can remove it with
array = ArrayUtils.removeElement(array, element), where element is the number that you were searching for.EDIT Actually, on second thought, the first approach is better. Using the second approach, it takes slightly more time to run.
Hope this helped!