Advertisement
Guest User

array targil 2

a guest
Nov 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. public static int[] getSameNumber(int[] array1, int[] array2)
  2. {
  3. //get the bigger array size
  4. int tempArraySize=0;
  5. if (array1.length>array2.length)
  6. {
  7. tempArraySize=array1.length;
  8. }
  9. else
  10. {
  11. tempArraySize=array2.length;
  12. }
  13. //create a temporary array
  14. int[] tempArray=new int[tempArraySize];
  15. //create a counter that will indicate total same numbers
  16. int arrayCounter=0;
  17.  
  18. //itreate on the entire array
  19. for (int cntArray1=0;cntArray1<array1.length;cntArray1+=1)
  20. {
  21. for (int cntArray2=0;cntArray2<array2.length;cntArray2+=1)
  22. {
  23. if (array1[cntArray1]==array2[cntArray2])
  24. {
  25. //add the found number to our array
  26. tempArray[arrayCounter]=array2[cntArray2];
  27. //increase array by 1
  28. arrayCounter+=1;
  29. }
  30. }
  31. }
  32.  
  33. //create return array in the excat same size of return numbers
  34. int[] returnArray=new int[arrayCounter];
  35. //iterate on the array to copy from temp array to our new array
  36. for (int counter=0;counter<arrayCounter;counter+=1)
  37. {
  38. returnArray[counter]=tempArray[counter];
  39. }
  40.  
  41. //return the array
  42. return returnArray;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement