Java > Array-3 > countClumps (CodingBat Solution)

Problem:

Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array.

countClumps({1, 2, 2, 3, 4, 4}) → 2
countClumps({1, 1, 2, 1, 1}) → 2
countClumps({1, 1, 1, 1, 1}) → 1


Solution:

public int countClumps(int[] nums) {
  boolean match = false;
  int count = 0;
  for (int i = 0; i < nums.length-1; i++) {
    if (nums[i] == nums[i+1] && !match) {
      match = true;
      count++;
    } 
    else if (nums[i] != nums[i+1]) {
      match = false;
    }
  }
  return count;
}


22 comments :

  1. public int countClumps(int[] nums) {
    int count = 0;
    for(int i = 0;i<nums.length-1;i++){
    if(nums[i] == nums[i+1]&&
    (i==0||nums[i-1]!=nums[i])){
    count++;
    }
    }
    return count;
    }

    ReplyDelete
  2. Your solution is excellent, but I do not understand it. Can you explain how you are using the boolean variable match? I do not understand its function.

    ReplyDelete
    Replies
    1. The match variable prevents the program from counting the same clump twice. For example, if the clump is of length 3 and we didn't check the match variable, count would be incremented twice for the same clump because nums[i] == nums[i+1] would be true for 2 iterations of the loop. The match variable tells the program that this matching clump has already been accounted for.

      Delete
  3. Thanks for the explanation, Shawn. Actually, I understand the purpose of the match variable. However, I do not know how to interpret !match. What does this mean if the variable is originally set at match = false? Does it mean if match == true?

    ReplyDelete
  4. Here is a better solution
    public int countClumps(int[] nums) {
    int count = 0;

    for(int i = 0; i < nums.length - 1; i++){
    if(nums[i] == nums[i+1])
    count++;

    while(i < nums.length-1 && nums[i] == nums[i+1])
    i++;
    }
    return count;
    }
    // Love Alpha

    ReplyDelete
  5. public int countClumps(int[] nums) {

    int count = 0;

    boolean match = Arrays.stream(nums).allMatch(s -> s==(nums[0]));

    if (nums.length == 0)

    return 0;

    else if (match)

    return 1;

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

    while (nums[i] == nums[i + 1]) {

    i++;

    count++;

    break;
    }
    }

    return count;

    }

    ReplyDelete
  6. Simplest code according to me....

    public int countClumps(int[] nums) {
    int count = 0;
    boolean chk=false;
    for (int i = 0; i < nums.length; i++) {

    if (i + 1 < nums.length) {
    if (nums[i] == nums[i+1] && !chk ) {
    count++;
    chk=true;

    }
    else if(nums[i]!=nums[i+1]){
    chk=false;
    }

    }
    }
    return count;
    }

    ReplyDelete
  7. public int countClumps(int[] nums) {
    int counter = 0;
    for(int i = 0; i< nums.length;i++){
    if(i < nums.length-1 && nums[i]==nums[i+1]){
    if(i==0){
    counter++;
    } else {
    if(nums[i-1]!=nums[i]){
    counter++;
    }
    }
    }
    }
    return counter;
    }

    ReplyDelete
  8. public int countClumps(int[] nums) {
    int count=0;
    for(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;}


    }
    return count;
    }

    ReplyDelete
  9. public int countClumps(int[] nums) {
    int count=0;
    for(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;}


    }
    return count;
    }

    ReplyDelete
  10. public int countClumps(int[] nums) { int count=0; for(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;} } return count; }

    ReplyDelete
  11. int count=0;
    for(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;}
    } return count; }

    ReplyDelete
  12. public int countClumps(int[] nums) {
    int len = 1;
    int count = 0;
    for (int i = 1; i < nums.length; i++) {
    len = nums[i] == nums[i - 1] ? len + 1 : 1;
    count = len == 2 ? count + 1 : count;
    }
    return count;
    }

    ReplyDelete
  13. public int countClumps(int[] nums) {
    int count = 0;
    for (int i = 0; i < nums.length - 1; i++)
    {
    if (nums[i] == nums[i + 1])
    {
    i++;
    count++;
    while (i < nums.length - 1 && nums[i] == nums[i + 1])
    {
    i++;
    }
    }
    }
    return count;
    }

    ReplyDelete
  14. public int countClumps(int[] nums) {
    int count = 0;
    int len = nums.length;

    for(int i = 0; i<len-1; i++){
    if(i==0 && nums[i]==nums[i+1]){
    count++;
    i++;
    }
    if(nums[i]==nums[i+1] && nums[i] != nums[i-1]){
    count++;
    i++;
    }
    }
    return count;
    }

    ReplyDelete
  15. int count = 1;
    int k = 0;
    for(int i = 0; i< nums.length-1; i++) {
    if(nums[i] == nums[i+1]) {
    k +=2;
    } else {
    if(k>=2) count++;
    k = 0;
    }
    }
    if(k == 0) return 0;
    return count;

    ReplyDelete
  16. public int countClumps(int[] nums) {

    int count=0;
    boolean readytocount=false;
    for(int i=0;i<nums.length-1;i++){

    if( nums[i]==nums[i+1]){
    if(readytocount || count==0){
    count++;
    readytocount=false;
    }
    }else{
    readytocount=true;
    }
    }
    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