I am implementing a set, using an array as the backend.Here is how I declared it (and some method implementations):public class Set {            private int[] elementData;            private int size;               ....          public void remove(int value) {             for(int count = 0; count < size; count ++) {                    if(elementData[count] == value) {                        elementData[count] = elementData[size - 1];                        size --;                        break;                    }               }           }          public boolean contains(int value) {             for(int count = 0; count < size; count ++) {                 if(elementData[count] == value) {                    return true;                 }            }            return false;           }   }I tested these methods with the JUnit testing framework and they both ran fine and worked. However I am trying to improve code reuse because I see the same iteration and a test condition in both methods, that is for(int count = 0; count < size; count ++) {            if(elementData[count] == value) {But both methods perform different operations if they make it into a conditional block, one returns while the other moves elements and then breaks. Is there a way to reduce code redundancy here or is that not necessary?
Is there some way of taking advantage of code reuse in this example?
java;algorithm;array;set
I would recommend creating an indexOf method, that returns the index of the value found.private boolean indexOf(int value) {    for (int i = 0; i < size; i++) {        if (elementData[i] == value) {            return i;        }    }    return -1;}Then you can check if indexOf returns something other than -1. If it does, use the index returned to do what you need to do.Additionally, you should really look over your indentation. And count is a bad name for a loop variable as count means the same thing as size. Use i or index as a loop variable instead.