# Count frequency of numbers in a phone number using ArrayList

Page 1 of 1

## 11 Replies - 6907 Views - Last Post: 30 January 2013 - 08:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=309995&amp;s=b96cf2c9c41f6fc44ad72cacb1069b5c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dfl2m

Reputation: 0
• Posts: 11
• Joined: 05-December 12

# Count frequency of numbers in a phone number using ArrayList

Posted 28 January 2013 - 08:43 PM

I'm trying to write a program that will count the frequency of numbers in a phone number using ArrayList. The phone number must be input as a String. I think I have it right, but the counts are coming out messed up. Attached is my code:

```import java.util.ArrayList;
import java.util.Scanner;

public class CharacterFrequency {

public static void arrayDigits(String number){
int size = number.length();
ArrayList<Integer> result = new ArrayList<Integer>(size);
for (int i = 0; i < number.length(); i++){
int element = number.charAt(i) - '0';
}
countDigits(result);
}

public static void countDigits(ArrayList<Integer> array){
int size = array.size();
for (int i = 0; i < size; i++){
int position = i;
int count = 0;
for (int j = 0; j < size; j++){
int element = array.get(j);
if (position == element){
count++;
array.set(j, count);
}
}
System.out.println("The number of " + i + " is " + array.get(i));
}
}

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);
String phoneNumber = keyboard.next();
arrayDigits(phoneNumber);

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Count frequency of numbers in a phone number using ArrayList

### #2 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 01:38 AM

Here's a simple way to count the number of times a digit shows up in an array.

```int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2};
int[] frequency = new int[10];

for(int number : phoneNumber) {
++frequency[number];
}

int count = 0;

for (int freq : frequency) {
System.out.println(count + " occurs "+   freq + " times.");
count++;
}

```

This post has been edited by farrell2k: 30 January 2013 - 03:52 PM

### #3 dfl2m

Reputation: 0
• Posts: 11
• Joined: 05-December 12

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 06:08 AM

farrell2k, on 29 January 2013 - 02:38 AM, said:

Here's a simple way to count the number of times a digit shows up in an array.

```int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2};
int[] frequency = new int[phoneNumber.length];

for(int number : phoneNumber) {
++frequency[number];
}

int count = 0;

for (int freq : frequency) {
System.out.println(count + " occurs "+   freq + " times.");
count++;
}

```

I know how to do it that way, which i have, but I need to do it using ArrayList.

### #4 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 08:48 AM

You have an array of integers and you can't imagine how to substitute its values instead???

### #5 CasiOo

• D.I.C Lover

Reputation: 1479
• Posts: 3,301
• Joined: 05-April 11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 08:59 AM

farrell2k, on 29 January 2013 - 08:38 AM, said:

Here's a simple way to count the number of times a digit shows up in an array.

```int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2};
int[] frequency = new int[phoneNumber.length];

for(int number : phoneNumber) {
++frequency[number];
}

int count = 0;

for (int freq : frequency) {
System.out.println(count + " occurs "+   freq + " times.");
count++;
}

```

It should be
```int[] frequency = new int[10];

```

```int[] frequency = new int[phoneNumber.length];

```

You really should be able to take his code, and just replace the array with an ArrayList You would then need to use the get and set methods of ArrayList

This post has been edited by CasiOo: 29 January 2013 - 09:01 AM

### #6 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:04 AM

CasiOo, on 29 January 2013 - 03:59 PM, said:

farrell2k, on 29 January 2013 - 08:38 AM, said:

Here's a simple way to count the number of times a digit shows up in an array.

```int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2};
int[] frequency = new int[phoneNumber.length];

for(int number : phoneNumber) {
++frequency[number];
}

int count = 0;

for (int freq : frequency) {
System.out.println(count + " occurs "+   freq + " times.");
count++;
}

```

It should be
```int[] frequency = new int[10];

```

```int[] frequency = new int[phoneNumber.length];

```

I do not understand why.

### #7 CasiOo

• D.I.C Lover

Reputation: 1479
• Posts: 3,301
• Joined: 05-April 11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:08 AM

This could have been my phone number
8 and 9 are out of bounds
```
public class Snippet {

/**
* @param args
*/
public static void main(String[] args) {
int[] phoneNumber = { 1, 2, 3, 4, 1, 5, 8, 9 };
int[] frequency = new int[phoneNumber.length];

for(int number : phoneNumber) {
++frequency[number];
}

int count = 0;
for (int freq : frequency) {
System.out.println(count + " occurs "+   freq + " times.");
count++;
}
}

}

```

Here they are not
```
public class Snippet {

/**
* @param args
*/
public static void main(String[] args) {
int[] phoneNumber = { 1, 2, 3, 4, 1, 5, 8, 9 };
int[] frequency = new int[10];

for(int number : phoneNumber) {
++frequency[number];
}

int count = 0;
for (int freq : frequency) {
System.out.println(count + " occurs "+   freq + " times.");
count++;
}
}

}

```

### #8 dfl2m

Reputation: 0
• Posts: 11
• Joined: 05-December 12

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:20 AM

farrell2k, on 29 January 2013 - 09:48 AM, said:

You have an array of integers and you can't imagine how to substitute its values instead???

I think I have use the .set option after creating the frequency array.

so it would have to be something like
```ArrayList<Integer>frequency = new ArrayList<Integer>(size);
int count = 0;
for (int element: originalArray){
frequency.set(element, count++);
}

```

i think.

i know how to do it when building an array using int [] whatever = new int[10], etc. but just not using ArrayList.

### #9 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:23 AM

Haha. Nice catch, CasiOo. />

```        ArrayList<Integer> number...
int[] frequency = new int[10]; //thanks CasiOo.

for(int number : number) {
++frequency[number];
}

int count = 0;

for (int freq : frequency) {
System.out.println(count + " occurs "+   freq + " times.");
count++;
}

```

Unless I am way off in my thinking, which is completely possible, this will work.

This post has been edited by farrell2k: 30 January 2013 - 03:53 PM

### #10 CasiOo

• D.I.C Lover

Reputation: 1479
• Posts: 3,301
• Joined: 05-April 11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:31 AM

dfl2m, on 29 January 2013 - 04:20 PM, said:

farrell2k, on 29 January 2013 - 09:48 AM, said:

You have an array of integers and you can't imagine how to substitute its values instead???

I think I have use the .set option after creating the frequency array.

so it would have to be something like
```ArrayList<Integer>frequency = new ArrayList<Integer>(size);
int count = 0;
for (int element: originalArray){
frequency.set(element, count++);
}

```

i think.

i know how to do it when building an array using int [] whatever = new int[10], etc. but just not using ArrayList.

Almost. The ArrayList should contain how many times a number has occurred
So if the number 1 occurs 5 times, the follow should be true
```frequency.get(1) == 5

```

Will this be the case with your own code? No!
You are using the same count variable for all the numbers
What you are currently counting is how many numbers there are, not the individual count of them =o

I think farrell2k is drunk right now, he doesn't seem like his normal self right now

### #11 dfl2m

Reputation: 0
• Posts: 11
• Joined: 05-December 12

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:32 AM

CasiOo, on 29 January 2013 - 10:29 AM, said:

dfl2m, on 29 January 2013 - 04:20 PM, said:

farrell2k, on 29 January 2013 - 09:48 AM, said:

You have an array of integers and you can't imagine how to substitute its values instead???

I think I have use the .set option after creating the frequency array.

so it would have to be something like
```ArrayList<Integer>frequency = new ArrayList<Integer>(size);
int count = 0;
for (int element: originalArray){
frequency.set(element, count++);
}

```

i think.

i know how to do it when building an array using int [] whatever = new int[10], etc. but just not using ArrayList.

Almost. The ArrayList should contain how many times a number has occurred
So if the number 1 occurs 5 times, the follow should be true
```frequency.get(1) == 5

```

Will this be the case with your own code? No!
You are using the same count variable for all the numbers
What you are currently counting is how many numbers there are, not the individual count of them =o

okay, i will have to look at my code later b/c i'm at work.

### #12 dfl2m

Reputation: 0
• Posts: 11
• Joined: 05-December 12

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 30 January 2013 - 08:31 AM

Okay it seems i figure it out

```	for (int i = 0; i < frequencySize; i++){
int count = 0;  //reset this after every iteration of the outer loop
for (int j = 0; j < arraySize; j++){
if (i == array.get(j)){  //for example, if i = 0, and array.get(j) = 0, then
count++;            //incremement count by 1
frequency.set(i, count); // set frequency(0, 1)
}
}
System.out.println("The number of " + i + " is " + frequency.get(i));
}

```