Java > Warmup-2 > arrayCount9 (CodingBat Solution)

Problem:

Given an array of ints, return the number of 9's in the array.

arrayCount9({1, 2, 9}) → 1
arrayCount9({1, 9, 9}) → 2
arrayCount9({1, 9, 9, 3, 9}) → 3


Solution:

public int arrayCount9(int[] nums) {
  int counter = 0;
  
  for (int i = 0; i < nums.length; i++){
    if (nums[i] == 9)
      counter++;
  }
  return counter;
}


1 comment :

  1. public int arrayCount9(int[] nums) {

    int count = 0;

    for(int i: nums) {

    if(i == 9)
    count++;
    }
    return count;
    }

    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