1

Write a Java class that has a static method named count that accepts a 2D-Array of integers and a target integer value as parameters and returns the number of occurrences of the target value in the array. For example, if a variable named list refers to an array containing values {{3,5,7,94}{5,6,3,50}} then the call of count(list, 3) should return 2 because there are 2 occurrences of the value 3 in the array.

Here is my coding and it's not giving me proper output P.S :-I have been told to take count method as public not static

class java
{
    public int count(int [,] list,int n)
    {
        int c = 0;
        for (int i = 0; i <list.Length; i++)
        {
            for (int j = 0; j < list.Length; j++)
            {
                if (list[i, j] == n)
                {
                    c++;
                }
            }
        }
        return c;
    }

class Program
{
    static void Main(string[] args)
    {
        java jv = new java();
        int[,] arr = { { 3, 5, 7, 94 }, {5, 6, 3, 50 } };
        int k=0;
        jv.count(arr,k);
    }
}
Tamir Vered
  • 10,187
  • 5
  • 45
  • 57
M Saad
  • 17
  • 6

4 Answers4

2

Iterating Multi-Dimensional arrays requires you to iterate each dimension with it's own Length, which means i and j should be 0-3 and 0-1 respectively.

as can be seen in the picture but for a different dimensioned array:

Multi-Dimensional Array

GetLength(0) would return 4.

GetLength(1) would return 3.

What you are doing is iterating them when i = (0 to Length) over j = (0 to Length) when Length = Height * Width = 8 in your case which means 8 over 8.

So your count() method should look like that:

public int count(int[,] list,int n)
{
    int c = 0;
    for (int i = 0; i < list.GetLength(0); i++)
    {
        for (int j = 0; j < list.GetLength(1); j++)
        {
            if (list[i, j] == n)
            {
                c++;
            }
        }
    }
    return c;
}

if you would like to iterate the array as an array of arrays instead of getting things complicated with "Which dimension am I iterating now?" you can use Jagged Arrays (Of course there are more things to consider about it), this will allow you to replace the whole method with this one short Linq:

public int count(int[][] list,int n)
{
    return list.SelectMany(x => x).Count(x => x == n);
}

or:

public int count(int[][] list, int n)
{
    return list.Sum(x => x.Count(y => y == n));
}
Community
  • 1
  • 1
Tamir Vered
  • 10,187
  • 5
  • 45
  • 57
1

Note the i against j in inner for. And do use i <list.GetLength(0) and j < list.GetLength(1) against list.Length

class java
{
    public int count(int [,] list,int n)
    {
        int c = 0;
        for (int i = 0; i < list.GetLength(0); i++)
        {
            for (int j = 0; j < list.GetLength(1); j++)
            {
                if (list[i, j] == n)
                {
                    c++;
                }
            }
        }
        return c;
    }

class Program
{
    static void Main(string[] args)
    {
        java jv = new java();
        int[,] arr = { {3,5,7,94 }, {5,6,3,50 } };
        int k=5;
        Console.WriteLine(jv.count(arr,k));
    }
}
Roman Dibikhin
  • 856
  • 4
  • 15
0

Since Array has implemented IEnumerable you can simply use foreach loop here (feel free to change static to instance method) :

    public static int count(int[,] list, int n)
    {
        int c = 0;
        foreach (var item in list) if (item == n) c++;
        return c;
    }

Usage:

  static void Main()
    {
        var r = count(new int[,]{
            {
                5, 8, 7, 8
            }, 
            {
                0, 8, 9, 3
            }}, 8);

        Console.WriteLine(r);

output : 3

P.S. Generally if possible, it is best to use a for loop as it is faster than foreach but in these case i like that if you use foreach you don't have nested for loops nor GetLength(x) calls it's just one line of code and it has almost same performance...

Fabjan
  • 13,506
  • 4
  • 25
  • 52
-1

Your error is in this line:

for (int j = 0; i < list.Length; j++)

It should be

for (int j = 0; j < list.Length; j++)
EvilTak
  • 7,091
  • 27
  • 36