1

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!

1
  • I haven't tried anything yet, just want to know if anyone can tell which is the fasted way of doing this. Commented Dec 10, 2011 at 14:32

6 Answers 6

1

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!

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

1 Comment

Be sure to use a LinkedList, though. Finding and emoving an element from a LinkedList is on average about 20 % faster than finding and removing an element from an ArrayList!
1

you can try something like below

if(list.contains(element)){
  index = list.indexOf(element);
  list.remove(index);
}

this will not need any iteration or looping.

1 Comment

Note that this will only find and remove the first "instance" of the number in the array. If the array contains a number more than once then you need to repeat. Also, it might be a bit more efficient to skip the list.contains() and go int index = list.indexOf(element); if (index >= 0) list.remove(index);
1

Adding onto @dku.rajkumar's post, if you insist on it being an array you can do something along this line:

Integer[] array = {1, 3, 7, 9, 13, 18, 25, 27, 35, 38, 41, 44, 46, 51, 68, 86, 99};
List<Integer> list = Arrays.asList(array);
int index = 0;
if(list.contains(value)){
    index = list.indexOf(value);
    list.remove(index);
    array = list.toArray(array);
}

array must be an Integer[] not an int[], as asList() uses generics and int can't be passed while Integer can. if it's int[] then you'll end up with List instead of List.

Comments

1

Well, you should probably use a ArrayList instead of an array, that way you can easily delete indexes as Lists are dynamic.

Then, you should just use a for loop to loop for each index of the ArrayList and check if it is equal to a number; deleting the current index if it is!

You could always convert your Array to an ArrayList using the asListmethod before you start looping, and then change it back using ArrayList.toArray() when you have finished!

Comments

0

If you are using an array, that will require creating a new array and copying the non-deleted values into that new array. The reason is that the size of an array can't be modified.

You would be better off using an implementation of List, for example an ArrayList. Those can change their size without you worrying about the details of it.

Comments

0

you can delete the value of array by putting that array into the List and that list in to the Array list like the following code:

Integer[] intArray = {1, 2, 3, 42}; // cannot use int[] here
        List<Integer> intList = Arrays.asList(intArray);

        ArrayList<Integer> aa= new ArrayList<Integer>(intList);

        Iterator<Integer> it1 = aa.iterator();
        while(it1.hasNext())
        {
            System.out.println(it1.next());

        }

        aa.remove(0);

        System.out.println("-------------------------------------");

        for(Integer a3:aa)
        {
            System.out.println(a3);

        }

        intArray=aa.toArray(new Integer[aa.size()]);

        System.out.println("-------------------------------------");


        for(Integer a4:intArray)
        {
            System.out.println(a4);

        }

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.