Advertisement
TheRealMyth

twoTimes

May 17th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. public class twoTimes{
  2. public static boolean twoTimes(int[] arr){
  3. int counter=0;
  4. // counts how often the current arrayfields number is in the entire array
  5. int positionA=0;
  6. //current position of the individual run through the array
  7. int positionB=0;
  8. //position of the current number in the array
  9. int currentNumber=arr[positionB];
  10. //deklaration of the first number thats to compare
  11. while((counter<=2)&&(positionB<arr.length)){
  12. currentNumber=arr[positionB];
  13. //loop is cancelled, if a number is more than 2 times in the array, or if every number has been compared to the rest of the array
  14. if(arr[positionA]==currentNumber){
  15. counter++;
  16. positionA++;
  17. //if the currently checked arrayfield contains the current number, the counter is increased
  18. }
  19. else {positionA++;
  20. }
  21. if(positionA==arr.length){
  22. if ((counter==2)&&(positionB<arr.length)){
  23. counter=0;
  24. positionB++;
  25. positionA=0;}
  26. else if(counter==1){
  27. counter=3;
  28. positionA=0;
  29. }
  30. }
  31. }
  32. return (counter==2);}
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement