hello can you help me how can i count the element in array

heres my code

int [] num = new int [10];
  int i;

 System.out.print("Please input 10 numbers");
  for(i=1;i<=10;i++)
   {
 
     num[i]= console.nextInt();
     System.out.print(" " + num[i]);
 
   }
   
    System.out.print("\n");
    System.out.print("The elements are");
    
     for(i=1;i<=10;i++)
      {
        System.out.print("The elements are: " +    num[i]);
       
      }

how can i count the elements in array example if I input this..10 1 1 1 2 2 5 5 5 6

the output should this it will count the elements if how many times this inputed

10= 1
1=3
1= 3
1=3
2=2
2=2
5=3
5=3
5=3
6=1


please help me... hoping for your positive responds...thank you in advance....

Recommended Answers

All 16 Replies

You need to use another collection of some type to accumulate the counts for each of the elements in the array.
One class to use could be a Hashtable with the element as the Key and the value as the count.

For a small range of integers, you could use an array of int to hold the counts. The element itself would be the index into this second array.

You need to use another collection of some type to accumulate the counts for each of the elements in the array.
One class to use could be a Hashtable with the element as the Key and the value as the count.

For a small range of integers, you could use an array of int to hold the counts. The element itself would be the index into this second array.

hello thank you for the reply, can you show me how to do this i don't really have idea please help me hoping for your positive responds...thank you in advance

Sorry, we don't do homework. Pick one of the solutions and try to program it.

Sorry, we don't do homework. Pick one of the solutions and try to program it.

sir but i don't know how to do this because i have no idea and i can't understand..please help me ...

You need to use another collection of some type to accumulate the counts for each of the elements in the array.
One class to use could be a Hashtable with the element as the Key and the value as the count.

For a small range of integers, you could use an array of int to hold the counts. The element itself would be the index into this second array.

hello sir correct me if i am wrong

int [] num = new int [10];

int i;
int count = 0;
int find;

 

System.out.print("Please input 10 numbers");

for(i=1;i<=10;i++)

{



num[i]= console.nextInt();

System.out.print(" " + num[i]);

 

}

 

System.out.print("\n");

System.out.print("The elements are");

 

for(i=1;i<=10;i++)

{
  
     if (num[i].length == find)
      {
         count++;
       }

   System.out.print("The elements are: " + num[i]);
   System.out.print( num[i] + "=" + count );
   
}

sir is this what you mean please correct me if i am wrong hoping for your positive responds....thank you in advance

#
for(i=1;i<=10;i++)
#

#
{

#
 
#
if (num[i].length == find)
#
{
#
count++;
#
}
#
 
#
System.out.print("The elements are: " + num[i]);
#
System.out.print( num[i] + "=" + count );
#
 
#
}

it doesn't make any sense..
first you should study collection types(map,list..)
otherwise you can accomplish this simply using for loop.

1.store all inputs in one array.
2.count recurrence values in that array.
3.then display total count of recurrence with respective value.
i can directly give solution to you..but its not our duty..those kind of help discouraged here.

It's a nice and crisp solution, but I cannot pass a parameter to the
function as I never know what elements the array holds.

Essentiall, I was wondering if the newer versions of javascript had
something similar to (and oh-so-useful) Perl's

$count{$val}++;

vivifying the hash by creating the key (the unknown element) and value (its
count).

@jemz - Can you add some comments to your code describing what it is trying to do?
For example, what is this supposed to do:

if (num[i].length == find)

hi,

please check with below code.

int [] num = new int [10];
int i;

for(i=1;i<10;i++)
{

num= i;

}

System.out.println("The elements size: " + num.length);

hello can you help me how can i count the element in array

heres my code

int [] num = new int [10];
  int i;

 System.out.print("Please input 10 numbers");
  for(i=1;i<=10;i++)
   {
 
     num[i]= console.nextInt();
     System.out.print(" " + num[i]);
 
   }
   
    System.out.print("\n");
    System.out.print("The elements are");
    
     for(i=1;i<=10;i++)
      {
        System.out.print("The elements are: " +    num[i]);
       
      }

how can i count the elements in array example if I input this..10 1 1 1 2 2 5 5 5 6

the output should this it will count the elements if how many times this inputed

10= 1
1=3
1= 3
1=3
2=2
2=2
5=3
5=3
5=3
6=1


please help me... hoping for your positive responds...thank you in advance....

Hey Check this code
this might help u out...
not optimized but still works

import java.io.*;
class fre
{
	public static void main(String args[])throws Exception
	{
		DataInputStream n = new DataInputStream(System.in);
		int a [] = new int[10], c=0,i,d,j;
		System.out.println("Enter number:");
		for(i=0;i<10;i++)
			a[i]=Integer.parseInt(n.readLine());
		for(i = 0; i< a.length; i++)
		{
			d = a[i];
			for(j=0;j<a.length;j++)
				if(d==a[j])
					c++;
			System.out.println(d+" occured: "+ c+" times");
			c=0;
		}
	}
}

Are you trying to teach the OP how to program?
Or do you just want to help him pass his course?

@jemz - Can you add some comments to your code describing what it is trying to do?
For example, what is this supposed to do:

if (num[i].length == find)

hello sir thank you for the reply...sir i want to know if how many times number of occurrences of elements in an array..my idea is this if (num.length == find)
but it's not working...example this are my inputed elements{1,1,2,2,2,3,4,5,8,8}

so it must be like this counting elements occurences
the elements 1 occurences is twice,and the elements 2 occurences 3 times...and so on...

1=2
1=2
2=3
2=3
2=3
3=1
4=1
5=1
8=2
8=2

can you help on this sir im still beggener on this sir i want to know more on this array...can you help me sir hoping for your positive responds...

You need to use another collection of some type to accumulate the counts for each of the elements in the array.
One class to use could be a Hashtable with the element as the Key and the value as the count.

For a small range of integers, you could use an array of int to hold the counts. The element itself would be the index into this second array.

sir i don't know about u said hashtable...hoping for your positive responds...

Hey Check this code
this might help u out...
not optimized but still works

import java.io.*;
class fre
{
	public static void main(String args[])throws Exception
	{
		DataInputStream n = new DataInputStream(System.in);
		int a [] = new int[10], c=0,i,d,j;
		System.out.println("Enter number:");
		for(i=0;i<10;i++)
			a[i]=Integer.parseInt(n.readLine());
		for(i = 0; i< a.length; i++)
		{
			d = a[i];
			for(j=0;j<a.length;j++)
				if(d==a[j])
					c++;
			System.out.println(d+" occured: "+ c+" times");
			c=0;
		}
	}
}

hello sir thank you for giving me this idea...and sir can you please put some comments on your code sir so that it can enlighten more in my mind and to be usefull for me, not only me but to the people who need this...so that many more people you will help and to be useful to them...i will try this sir i will write again if i have some doubt...thank you again sir....more power to you...

Are you trying to teach the OP how to program?
Or do you just want to help him pass his course?

hello sir what is OP?...sir i am still beginer on this java sir and i want to learn...that's why i need your help sir to teach me more on this....by the way it's not my home work...i am just practicing to enhance my programming skill...hoping for your positive responds...

Hey Check this code
this might help u out...
not optimized but still works

import java.io.*;
class fre
{
	public static void main(String args[])throws Exception
	{
		DataInputStream n = new DataInputStream(System.in);
		int a [] = new int[10], c=0,i,d,j;
		System.out.println("Enter number:");
		for(i=0;i<10;i++)
			a[i]=Integer.parseInt(n.readLine());
		for(i = 0; i< a.length; i++)
		{
			d = a[i];
			for(j=0;j<a.length;j++)
				if(d==a[j])
					c++;
			System.out.println(d+" occured: "+ c+" times");
			c=0;
		}
	}
}

hello sir i tried your code and it works...thank you for giving me this idea...can you please some comments on your code so that it can help me more...thank you sir ...hoping for your positive responds....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.