Ankit Katiyar
Ankit Katiyar

Reputation: 3001

Find the count occurrence of a given number in a list best solution

I have written a solution to find the occurrence of a common number in a n lists. I am just concerned if this is the best solution? Please suggest the best way to do it.

import java.util.Arrays;
import java.util.List;

public class CommonNumberFinder
{

    final static List<Number> list1 = Arrays.asList(new Number[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    final static List<Number> list2 = Arrays
            .asList(new Number[] { 3, 5, 1, 6, 2, 6, 3, 3, 6, 1, 5, 7, 2, 6, 2, 5, 2, 6, 8, 2 });

    public static void main(String[] args)
    {
        findCountOfNumber(Arrays.asList(new List[] { list1, list2 }), 6);
    }

    private static void findCountOfNumber(final List<List<Number>> lists, final Number num)
    {
        int count = 0;
        for (List<Number> list : lists)
        {
            if (!list.contains(num))
            {
                System.err.printf("Number %d is not common", num);
            }
        }
        for (List<Number> list : lists)
        {
            for (Number number : list)
            {
                if (number == num)
                    count++;
            }
            System.out.println("List have-" + count);
            count = 0;
        }

    }
}

Upvotes: 1

Views: 76

Answers (4)

Sankalp Bhagat
Sankalp Bhagat

Reputation: 7

you could properly specify the count with the particular list as

private static void findCountOfNumber(final List<List<Number>> lists, final Number num) {
    int count = 0, listNumber = 0;
    for (List<Number> list : lists) {
        listNumber++;
        count = Collections.frequency(list, num);
        if (count == 0) {
            System.err.printf("Number %d is not common in list %d", num, listNumber);
        } else {
            System.out.println("List " + listNumber + " has count : " + count);
        }
    }
}

Upvotes: 1

Roberto Attias
Roberto Attias

Reputation: 1903

In case you can't use the function mentioned by Itamar (as it would probably be the case if this is an assignment), I'm adding a simplified version below:

private static void findCountOfNumber(final List<List<Number>> lists, final Number num)
{
    for (List<Number> list : lists)
    {
        int count = 0;
        for (Number number : list)
        {
            if (number == num)
                count++;
        }
        if (count == 0) {
            System.err.printf("Number %d is not common", num);
        } else {
            System.out.println("List have-" + count);
        }
    }

}

Upvotes: 0

ItamarG3
ItamarG3

Reputation: 4122

Much simpler.

There's a function for that already in Java:

Collections.frequency(list,num);

So in your case you'd have to do a loop:

for(List<Number> nestedList : lists){
    count+=Collections.frequency(nestedList,num);
}
if(count == 0) {                
   System.err.print(num+ " is not common");
} else{
   System.out.println("List have-" + count);
}

That would give the desired output

Upvotes: 2

Saurav Sahu
Saurav Sahu

Reputation: 13924

Your code is incorrectly handling the count variable, as you are not resetting it for each list.

Optimisation over your code:

for (List<Number> list : lists)
{
    count = Collections.frequency(list ,num);

    if(0 == count) {                
       System.err.printf("Number %d is not common", num);
    }else{
       System.out.println("List have-" + count);
    }
}

Upvotes: 0

Related Questions