-1
 int count = 0; 
    for (int i = 0; i < arr.length; i++) { 
        for (int j = 0; j < arr.length; j++) { 
         if (arr[i] == arr[j] && i != j) { 
            return false; 

         }else count++; 
      } 
    } return (count == 3); 

Task: Given an integer denoting the size of the array.Fill array with integers and return true if array contains duplicate 3 times next to each other. This code does not Works fine. Given true if found duplicates 2 times.I need three times,and next to each other!! Some1 could help me please?!

5
  • Why the deuce did you use an O(N * N) algorithm? Commented Mar 14, 2017 at 13:13
  • @Abubakkar: no, no, no! Commented Mar 14, 2017 at 13:13
  • 5
    What's the problem with comparing the values at i, i+1 and i+2? Commented Mar 14, 2017 at 13:13
  • @Thomas: Absolutely Commented Mar 14, 2017 at 13:14
  • Think about what your code does: it iterates over the array n + 1 times, i.e. once for the outer loop and once for each of the n elements. You then increase count if either the indices are the same or the values are not. What do you think count would express in that case? Commented Mar 14, 2017 at 13:17

3 Answers 3

5

What's about this:

for (int i = 0; i < arr.length - 2; i++) { 
     if (arr[i] == arr[i + 1] && arr[i] == arr[i + 2]) { 
        return true; 
     }
}
return false;
Sign up to request clarification or add additional context in comments.

7 Comments

UV'd. Picky but I'd prefer i = 2; i < arr.length as that will work better in languages like C++ which tend to use unsigned types for sizes.
Thank you very much! You are very helpfuly!!
But,could you explain me,why i = 2 in for loop?
If i give n = 6(denoting the numbers of array) the program give me back error.And does not working.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Main.isContainDistinct(Main.java:26) at Main.main(Main.java:13)
|
1

Solution using ArrayList to store the matching numbers,

    int[] arryOne={1,2,3,4,4,4,5,5,5,6,6,7,7,7,8,9,9,9,19,19,19,21,21,30,45,10,10,10};
    ArrayList<Integer> arryTwo=new ArrayList<Integer>();
    for (int x=0;x<=arry.length-1;x++)
    {
        if(arryOne[x]==arryOne[x+1] && arryOne[x]==arryOne[x+2] &&  x!=arry.length-2)
        {
            arryTwo.add(arry[x]);
            x=x+2;

        }       
    }
    System.out.println("The number which is repeating three times in the array is = ");
    for (int valInArrayList: arryTwo)
    {
        System.out.print(valInArrayList + ", ");
    }

Comments

-1
for(int i=0;i<arr.length-2;i++){
   if((arr[i]==arr[i+1])&& (arr[i]==arr[i+2])){
      return true;
    }
}
return false;

This way, we only check the array once, and this improve performances. What we do here in plain english is : "Check if a number is equal to the two following him", and we stop before going out of the array (this is what array.length-2 is for)

Ex: for an array of 6 numbers, we will check

[0,1,2]  
[1,2,3] 
[2,3,4]  
[3,4,5]

3 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
If you remove the snarky comment at the beginning then I'm sure folk will retract downvotes. I will.
Downvote converted to upvote. Thank you for editing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.