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!
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!
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!
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!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.
int index = list.indexOf(element); if (index >= 0) list.remove(index);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.
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!
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.
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);
}