# How to implement deleteValues (int values) method for a custom ArrayList?

I am implementing my custom ArrayList class for integers with the help of an array, and I would like to be able to delete a certain value from my array. My problem is when there are many same delete-able value next to each other, I am getting two 0s next to each other which leads to a bug. i tried to solve it for a couple of hours without luck. Here is my code:

```    int max=10;
public int[] a = new int[max];

@Override
public void deleteValues(int value) {
int tempIndex=0;

for (int i = 0; i <50 ; i++) {
if (a[tempIndex] == value) {
a[tempIndex] = a[tempIndex + 1];
a[tempIndex + 1] = 0;
} else if (a[tempIndex] == 0) {
a[tempIndex] = a[tempIndex + 1];
a[tempIndex + 1] = 0;

} else {
tempIndex++;

}

}

}
```

My array looks like that before deleting the value (4):

```[4, 2, 3, 4, 4, 4, 4, 1, 2, 3]
```

This is the wrong result after running the code:

```[2, 3, 0, 0, 4, 4, 4, 1, 2, 3]
```

What I would like to achieve:[2, 3, 1, 2, 3, 0, 0, 0, 0, 0]

My question is: What would be the best approach to make the code work, using as few loop as possible?

## Answers

One of the problems in your code is that you're always copying the element at index tempIndex+1 into tempIndex: it's always the next element. In fact, after deleting let's say 5 elements from the array, you'll have to copy tempIndex+5 into tempIndex.

I think this is a good way of doing it:

```public void deleteValues(int[] a, int value) {
int j=0;
for(int i=0; i<a.length; i++) {
if(a[i]!=value) {
a[j] = a[i];
j++;
}
}
// fill the rest of the array with zeros
while(j<a.length) {
a[j] = 0;
j++;
}
}
```

Basically, you keep two indices: i and j. The index i follows the "original" array, while index j follows the "new" array (after deletion). Index i loops over all the elements: if a[i] is not equal to value, copy it into its new position j and increment both j and i. If a[i] is equal to value, skip it and increment i without incrementing j. After all the elements have been copied or skipped, fill the end of the array with zeros.

Sample input:

```a     = {4, 2, 3, 4, 4, 4, 4, 1, 2, 3}
value = 4
```

Output:

```a     = {2, 3, 1, 2, 3, 0, 0, 0, 0, 0}
```

```public static void deleteValues(int[] a, int value) {
int newSize = a.length;
int current = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != value) {
if (i != current) {
a[current] = a[i];
newSize--;
}
current++;
}
}
//use first newSize values, for example you can copy to new array
System.out.println("New size = " + newSize);
}
```

you can use iterator:

```List<Integer> numbers = ....
Iterator<Integer> i = numbers.iterator();
while (i.hasNext()) {
Integer num = i.next();
// add here your custom code
i.remove();
}
```

```            int tempIndex,index;
for (index = 0, tempIndex = 0; index < valuesArray.length; index++) {
if (valuesArray[index] != valToDelete) {
valuesArray[tempIndex++]=valuesArray[index];
}
}
while(tempIndex<valuesArray.length){
valuesArray[tempIndex++]=0;
}
```

### Working around IE8's broken Object.defineProperty implementation

Consider the following code, using ECMAScript5's Object.defineProperty feature:

### HTML emails - hiding content in Outlook.com accounts

I was using the following conditional statement to hide content in Outlook for responsive HTML emails,