Jump to content

Java: Count occurrence of the entered numbers?

trapizi

Hi guys, I'm new to Java and I'm just wondering if it is possible to count occurrence of integers without using array?

Hi

Link to comment
Share on other sites

Link to post
Share on other sites

You need one variable for one number to count, so if there would be input of 3 3 3 7 7 8 1 you would need 4 variables to count each number. 

When you say array, do you mean that you would need array that looks like [1, 0, 3, 0, 0, 0, 2, 1] to store your counters?

I would use associative array so it would look like {3 => 3, 7 => 2, 8 => 1, 1 => 1} so there is no unused space.

Link to comment
Share on other sites

Link to post
Share on other sites

What exactly are you trying to count? Do you have a text file with a bunch of numbers and you want to know how many matches there are to the number entered? 

 

Or do you want to see how many times every number that a user enters was entered by the user? 

 

Or....something else

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Without an array?

But...why?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, Nineshadow said:

Without an array?

But...why?

Without some kind of limit on the numbers you can't really use an array anyway. A dictionary / counter / map / whatever Java has would be the way to go.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×