Java > Array-2 > more14 (CodingBat Solution)

Problem:

Given an array of ints, return true if the number of 1's is greater than the number of 4's

more14({1, 4, 1}) → true
more14({1, 4, 1, 4}) → false
more14({1, 1}) → true


Solution:

public boolean more14(int[] nums) {
  int count1 = 0;
  int count4 = 0;
  boolean isTrue = false;
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 1)
      count1++;
    if (nums[i] == 4)
      count4++;
  }
  
  if (count1 > count4)
    isTrue = true;
  
  return isTrue;
    
}


14 comments :

  1. public boolean more14(int[] nums) {
    boolean result = true;
    int zero = 0;
    for (int i=0; i0) ? true : false;
    return result;
    }

    ReplyDelete
    Replies
    1. something is missing if this works it is legendary

      Delete
  2. Not sure why the formatting screwed up on previous comment...

    > public boolean more14(int[] nums) {
    > boolean result = true;
    > int zero = 0;
    > for (int i=0; i < nums.length; i++)
    > zero = (nums[i]==1) ? zero+1 : (nums[i]==4) ? zero-1 : zero;
    > result = (zero>0) ? true : false;
    > return result;
    > }

    ReplyDelete
  3. public boolean more14(int[] nums) {
    int count = 0;

    for (int i=0; i 0) return true;

    return false;
    }

    ReplyDelete
  4. public boolean more14(int[] nums) {

    int count1 = 0, count4 = 0;

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

    if(nums[i] == 1) count1++;
    if(nums[i] == 4) count4++;

    } return count1 > count4;

    }


    ReplyDelete
  5. public boolean more14(int[] nums) {

    int c1=0,c4=0;
    for (int i=0; ic4 ? true:false;
    }

    ReplyDelete
  6. public boolean more14(int[] nums) {
    int one = 0;
    int four = 0;

    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 1){
    one++;
    } else if(nums[i] == 4){
    four++;
    }
    }

    if(one > four){
    return true;
    } else {
    return false;
    }

    }

    ReplyDelete
  7. public boolean more14(int[] nums)
    {
    int valueOfFour = 0;
    int valueOfOne = 0;

    for(int i = 0; i < nums.length; i++)
    {
    if(nums[i] == 1) valueOfOne += 1;
    if(nums[i] == 4) valueOfFour += 1;
    }

    return(valueOfFour < valueOfOne);
    }

    ReplyDelete
  8. public boolean more14(int[] nums) {
    int count1=0;
    int count4=0;
    boolean check=false;
    for(int i=0; icount4){
    check=true;
    }
    return check;
    }

    ReplyDelete
  9. With Java Stream

    public static boolean more14(int[] nums) {

    long count1 = Arrays.stream(nums)
    .filter(i -> i == 1)
    .count();

    long count4 = Arrays.stream(nums)
    .filter(i -> i == 4)
    .count();

    return count1 > count4;
    }

    ReplyDelete
  10. public boolean more14(int[] nums) {
    int sum1=0; int sum2=0;
    for(int i=0; isum2)
    return true;
    return false;
    }

    ReplyDelete
    Replies
    1. public boolean more14(int[] nums) {
      int sum1=0; int sum2=0;
      for(int i=0; isum2)
      return true;
      return false;
      }

      Delete
  11. public boolean more14(int[] nums) {
    int count1 = 0;
    int count4 = 0;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 1) count1++;
    if(nums[i] == 4) count4++;
    }
    return count1 > count4;
    }

    ReplyDelete
  12. public boolean more14(int[] nums) {
    int oneCount = (int) Arrays.stream(nums)
    .filter(n -> n == 1).count();
    int fourCount = (int) Arrays.stream(nums)
    .filter(n -> n == 4).count();
    return oneCount > fourCount;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact