user3790782
user3790782

Reputation: 33

Check if every elements in array is of same value

Basically what I want to do is to check on each element in an array of int, if all elements are of the same value.

I create int array as below to pass to the method for comparing each array element, it return boolean true even tough the elements are not all the same values.

Int[] denominator = {3,3,4,3};

boolean compare;

compare = bruteforce(denominator);

public static boolean bruteforce(int[] input) {

int compare =0;
int count =0;

    for (int i = 0; i < input.length; i++) {

        compare = input[i];

        while(count<input.length){

            if(input[i+1]==compare){
            return true;

            }
            i++;
            count++;

        }//end while

    }//end for
    return false;
}//end method

I suppose the method above will loop for and keep compare for each element of the array.

When I print out the output, it showed that it only loop once, the return the boolean as true.

I really lost the clue what could be wrong in my code.

Perhaps I just overlook of some silly mistakes.

Upvotes: 3

Views: 18616

Answers (8)

Rakesh KR
Rakesh KR

Reputation: 6527

Try,

Integer[]    array = {12,12,12,12};
Set<Integer> set   = new HashSet<Integer>(Arrays.asList(array));
System.out.println(set.size()==1?"Contents of Array are Same":"Contents of Array are NOT same");

Explanation:

Add the array to a set and check the size os set , if it is 1 the contents are same else not.

Upvotes: 3

J0e3gan
J0e3gan

Reputation: 8938

You only need one loop and should return false as quickly as possible where applicable (i.e. when you encounter an element that doesn't match the first).

You also need to account for the edge cases that the input array is null or has one element.

Try something like this, which I minimally adapted from the code you provided...

public class BruteForceTest {

    public static boolean bruteforce(int[] input) {

        // NOTE: Cover the edge cases that the input array is null or has one element.
        if (input == null || input.length == 1)
            return true; // NOTE: Returning true for null is debatable, but I leave that to you.

        int compare = input[0]; // NOTE: Compare to the first element of the input array.

        // NOTE: Check from the second element through the end of the input array.
        for (int i = 1; i < input.length; i++) {
            if (input[i] != compare)
                return false;
        }

        return true;

    }

    public static void main(String[] args) {

        int[] denominator = {3,3,4,3};
        boolean compare = bruteforce(denominator);

        // FORNOW: console output to see where the first check landed
        System.out.print("{3,3,4,3}:\t");
        if (compare)
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

        // NOTE: a second array to check - that we expect to return true
        int[] denominator2 = {2,2};
        boolean compare2 = bruteforce(denominator2);

        System.out.print("{2,2}:\t\t");
        if (compare2)
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

        /*
         *  NOTE: edge cases to account for as noted below
         */

        // array with one element
        int[] denominator3 = {2};
        System.out.print("{2}:\t\t");
        if (bruteforce(denominator3))
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

        // null array
        System.out.print("null:\t\t");
        if (bruteforce(null))
            System.out.println("Yup!");
        else
            System.out.println("Nope!");

    }

}

...and outputs:

{3,3,4,3}:  Nope!
{2,2}:      Yup!
{2}:        Yup!
null:       Yup!

Upvotes: 2

Jae Heon Lee
Jae Heon Lee

Reputation: 1101

If you're interested in testing array equality (as opposed to writing out this test yourself), then you can use Arrays.equals(theArray, theOtherArray).

Upvotes: -1

Rhendz
Rhendz

Reputation: 101

public class Answer {

    public static void main(String[] args)
    {
        boolean compare = false;
        int count = 0;
        int[] denominator = { 3, 3, 4, 3 };


        for (int i = 0; i < denominator.length; i++)
        {
            if(denominator[0] != denominator[i]) 
            {
                count++;
            }
        }
        if(count > 0)
        {
            compare = false;
        } else
        {
            compare = true;
        }
        System.out.println(compare);

    }
}

One mistake that I noticed right of the bat was that you declared your array as Int[], which is not a java keyword it is in fact int[]. This code checks your array and returns false if the array possesses values that are not equal to each other. If the array possesses values that are equal to each other the program returns true.

Upvotes: 0

Jobs
Jobs

Reputation: 3377

Right now, you are not checking if "all the elements are of the same value". You are ending the function and returning true whenever (the first time) two elements are equal to each other. Why not set the boolean value to true and return false whenever you have two elements that are not equal to each other? That way you can keep most of what you have already.

if(input[i+1]!=compare) return false;

Upvotes: 0

gprathour
gprathour

Reputation: 15333

If you want to check if all elements are of same value then you can do it in a simpler way,

int arr = {3,4,5,6};

int value = arr[0];


flag notEqual = false;

for(int i=1;i < arr.length; i++){

     if(!arr[i] == value){
           notEqual = true;
           break;
     }

}

if(notEqual){
     System.out.println("All values not same");
}else{
     System.out.println("All values same);
}

Upvotes: 0

Codejunky
Codejunky

Reputation: 632

If an array elements are equal you only need to compare the first element with the rest so a better solution to your problem is the following:

public static boolean bruteforce(int[] input) {
     for(int i = 1; i < input.length; i++) {
         if(input[0] != input[i]) return false;
     }

     return true;
}

You don't need more than one loop for this trivial algorithm. hope this helps.

Upvotes: 2

dom
dom

Reputation: 321

If all elements are the same value, why not use only one for loop to test the next value in the array? If it is not, return false.

Upvotes: 0

Related Questions