Java > Array-2 > has22 (CodingBat Solution)

Problem:

Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.

has22({1, 2, 2}) → true
has22({1, 2, 1, 2}) → false
has22({2, 1, 2}) → false


Solution:

public boolean has22(int[] nums) {
  boolean found = false;
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 2 && i > 0 && nums[i-1] == 2) {
      found = true;
    }
    if (nums[i] == 2 && i < nums.length-1 && nums[i+1] == 2) {
      found = true;
    }
  }
  return found;
}


27 comments :

  1. whats wrong with
    public boolean has22(int[] nums) {
    if(nums.length<2) return false;
    for(int i=0; i<nums.length-1;i++){
    if(nums[i]==nums[i+1]) return true;
    }
    return false;
    }
    it will iterate over every element in the array and compare adjacent elements

    ReplyDelete
  2. public boolean has22(int[] nums) {

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

    ReplyDelete
  3. public boolean has22(int[] nums) {

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

    }
    }

    return false;
    }

    ReplyDelete
    Replies
    1. my sloution is

      public boolean has22(int[] nums) {
      boolean twofound=false;

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

      Delete
  4. public boolean has22(int[] nums) {
    boolean stop = false;

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

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

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

    if (nums[i]==2 && nums[i+1]==2) return true;
    }

    return false;
    }

    ReplyDelete
  6. public boolean has22(int[] nums) {

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

    ReplyDelete
  7. You don't need a loop to achieve that. Just by using indexOf, you can check the next index if 2 exists. Here is my solution with Javascript:
    const has22 = (givenArray) => {
    const indexOfTwo = givenArray.indexOf(2);
    if (indexOfTwo > -1 && givenArray[indexOfTwo + 1] === 2) {
    return true;
    }

    return false;
    }

    ReplyDelete
  8. public boolean has22(int[] nums) {
    boolean found22 = false;
    for(int i = 0; i<nums.length-1; i++){
    if(nums[i] == 2 && nums[i+1] == 2){
    found22 = true;
    break;
    }
    else found22 = false;
    }
    return found22;
    }

    ReplyDelete
  9. public boolean has22(int[] nums) {
    boolean found22 = false;
    for(int i = 0; i<nums.length-1; i++){
    if(nums[i] == 2 && nums[i+1] == 2){
    found22 = true;
    break;
    }
    else found22 = false;
    }
    return found22;
    }

    ReplyDelete
  10. public boolean has22(int[] nums) {
    return Arrays.toString(nums).contains("2, 2");
    }

    ReplyDelete
  11. public boolean has22(int[] nums) {
    for(int i = 0;i < nums.length -1;i++){
    if(nums[i] == 2 && nums[i+1] == 2) return true;
    }
    return false;
    }

    ReplyDelete
  12. public boolean has22(int[] nums) {

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

    ReplyDelete
  13. public boolean has22(int[] nums) {
    if(nums.length>0){
    for(int i=0; i<nums.length-1; i++){
    if(nums[i]==2 && nums[i+1]==2){
    return true;
    }
    }
    }

    return false;
    }

    ReplyDelete
  14. public boolean has22(int[] nums) {

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

    ReplyDelete
  15. public boolean has22(int[] nums) {

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

    ReplyDelete

  16. Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.

    ReplyDelete

  17. Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.

    public boolean has22(int[] nums) {
    boolean twofound=false;

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

    ReplyDelete
  18. public boolean has22(int[] nums) {

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

    ReplyDelete
  19. public boolean has22(int[] nums) {

    boolean twoTwo = false;

    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 2 && i < nums.length-1 && nums[i + 1] == 2){
    twoTwo = true;




    }
    }
    return twoTwo;
    }

    ReplyDelete
  20. public boolean has22(int[] nums) {

    boolean twoTwo = false;

    for(int i = 0; i < nums.length-1; i++){
    if(nums[i] == 2 && nums[i + 1] == 2){
    twoTwo = true;




    }
    }
    return twoTwo;
    }

    ReplyDelete
  21. public boolean has22(int[] nums)
    {
    for(int i = 1; i < nums.length; i++)
    {
    if(nums[i-1] == 2 && nums[i] == 2) return true;
    }

    return false;
    }

    ReplyDelete
  22. public boolean has22(int[] nums) {
    for(int i = 0; i < nums.length; i++){
    if(i < nums.length-1){
    if(nums[i] == 2 && nums[i+1] == 2) return true;
    }
    }

    return false;
    }

    ReplyDelete
  23. OR

    public boolean has22(int[] nums) {
    for(int i = 0; i < nums.length-1; i++){
    if(nums[i] == 2 && nums[i+1] == 2) return true;
    }

    return false;
    }

    ReplyDelete
  24. With Java Stream

    public static boolean has22(int[] nums) {

    boolean result = IntStream.range(0, nums.length-1)
    .anyMatch( i -> nums[i] == 2 && nums[i + 1] == 2);

    return result;
    }

    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