Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int[] getSameNumber(int[] array1, int[] array2)
- {
- //get the bigger array size
- int tempArraySize=0;
- if (array1.length>array2.length)
- {
- tempArraySize=array1.length;
- }
- else
- {
- tempArraySize=array2.length;
- }
- //create a temporary array
- int[] tempArray=new int[tempArraySize];
- //create a counter that will indicate total same numbers
- int arrayCounter=0;
- //itreate on the entire array
- for (int cntArray1=0;cntArray1<array1.length;cntArray1+=1)
- {
- for (int cntArray2=0;cntArray2<array2.length;cntArray2+=1)
- {
- if (array1[cntArray1]==array2[cntArray2])
- {
- //add the found number to our array
- tempArray[arrayCounter]=array2[cntArray2];
- //increase array by 1
- arrayCounter+=1;
- }
- }
- }
- //create return array in the excat same size of return numbers
- int[] returnArray=new int[arrayCounter];
- //iterate on the array to copy from temp array to our new array
- for (int counter=0;counter<arrayCounter;counter+=1)
- {
- returnArray[counter]=tempArray[counter];
- }
- //return the array
- return returnArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement