Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class twoTimes{
- public static boolean twoTimes(int[] arr){
- int counter=0;
- // counts how often the current arrayfields number is in the entire array
- int positionA=0;
- //current position of the individual run through the array
- int positionB=0;
- //position of the current number in the array
- int currentNumber=arr[positionB];
- //deklaration of the first number thats to compare
- while((counter<=2)&&(positionB<arr.length)){
- currentNumber=arr[positionB];
- //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
- if(arr[positionA]==currentNumber){
- counter++;
- positionA++;
- //if the currently checked arrayfield contains the current number, the counter is increased
- }
- else {positionA++;
- }
- if(positionA==arr.length){
- if ((counter==2)&&(positionB<arr.length)){
- counter=0;
- positionB++;
- positionA=0;}
- else if(counter==1){
- counter=3;
- positionA=0;
- }
- }
- }
- return (counter==2);}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement