ASUR
ASUR

Reputation: 405

Cannot count number in ArrayList using java

I am using the List as shown below:-

List<Integer> integerlist = new ArrayList<Integer>();

integerlist=primeFactors(numberToBERadical);

The primeFactors method return me some number when i sysOut as:-

System.out.println("integer list=="+integerlist);
integer list==[2, 2, 2, 3, 5]

My Problem is i want to count how many time that each number appears so that i can show Like this:-

2^3 * 3 * 5

Upvotes: 0

Views: 547

Answers (5)

KARASZI Istv&#225;n
KARASZI Istv&#225;n

Reputation: 31467

One possible, but not the simplest solution:

public Map<Integer, AtomicInteger> primeFactors(List<Integer integerList) {
  final Map<Integer, AtomicInteger> count = new HashMap<Integer, AtomicInteger>();

  for (final Integer number : integerlist) {
    if (count.containsKey(number)) {
      count.get(number).incrementAndGet();
    } else {
      count.put(number, new AtomicInteger(1));
    }
  }

  return count;
}

Upvotes: 1

Mansingh Shitole
Mansingh Shitole

Reputation: 151

List<Integer> inputList = new ArrayList<Integer>();

    inputList.add(2);
    inputList.add(2);
    inputList.add(2);
    inputList.add(3);
    inputList.add(3);
    inputList.add(4);

    Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();

    boolean flag = false;

    for(int val : inputList)
    {
        if(resultMap.get(val) == null)
        {
            resultMap.put(val, 1);
        }
        else
        {
            resultMap.put(val, (resultMap.get(val).intValue())+1);
        }
    }

    for(int key : resultMap.keySet())
    {
        if(resultMap.get(key) == 1)
        {
            if(!flag)
            {
                System.out.print(key);
                flag = true;
            }
            else
            {
                System.out.print("*"+key);
            }
        }
        else
        {
            if(!flag)
            {
                System.out.print(key+"^"+resultMap.get(key));
                flag = true;
            }
            else
            {
                System.out.print("*"+key+"^"+resultMap.get(key));
            }
        }
    }

    System.out.println();

Upvotes: 1

pcalcao
pcalcao

Reputation: 15990

You need to associate each factor with the number of times it occurs (the exponent).

To do that, a better data structure than a List would be a Map<Integer,Integer>.

Let's assume you still get a List from your primeFactors(numberToBERadical); (assuming you can't change it or don't want to for some reason).

You can do something like:

public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();

    list.add(2);
    list.add(2);
    list.add(2);
    list.add(3);
    list.add(3);
    list.add(5);

    Map<Integer, Integer> factors = new HashMap<>();
    for(Integer fact: list){
        if(!factors.containsKey(fact)) { factors.put(fact, 1);}
        else {
            Integer value = factors.get(fact);
            factors.put(fact, ++value);
        }
    }
    System.out.println(factors);
}

Prints: {2=3, 3=2, 5=1}

Upvotes: 3

Sebastian van Wickern
Sebastian van Wickern

Reputation: 1749

If the array is sorted, you can count them like:

int count = 0;
int last = 0; // zero is not possible for prime-factors
for(int i=0;i<list.size();i++) {
    if(i == 0 || last == list.get(i)) count++;
    else {
        if(last > list.get(0)) {
            System.out.print(" * ");
        }

        System.out.print(last + (count > 1 ? "^" + count: ""));
        count = 1;
    }
    last = list.get(i);
}
//print last sequence.
if(last > 0) {
    if(last > list.get(0)) {
        System.out.print(" * ");
    }

    System.out.print(last + (count > 1 ? "^" + count: ""));
}

Upvotes: 2

Boris the Spider
Boris the Spider

Reputation: 61178

You need to use a Map<Integer, Integer> which you can override the put method in in order to count the instances of each Integer.

final Map<Integer, Integer> myStringMap = new HashMap<>(){
 @override
 public String put(final Integer key, final Integer value) {
   if(contains(key)) {
     return put(key, get(key) + 1);
   } else {
     return put(key, 1);
   } 
 }
};

You could even use a TreeMap to have the prime factors sorted by size. I answered a very similar question here. Simply loop over your List and dump it into the map

for(final Integer integer : myList) {
  myCountingMap.put(integer, 1);
}

Even better would be to change your factorising method to return a Map to begin with.

Upvotes: 1

Related Questions