user1215225
user1215225

Reputation: 29

How can I avoid repetition of the same number?

This is what I want :
Let the user enter as many numbers as they want until a non number is entered (you may assume there will be less than 100 numbers). Find the most frequently entered number. (If there are more than one, print all of them.)
Example output:
Input: 5
Input: 4
Input: 9
Input: 9
Input: 4
Input: 1
Input: a
Most common: 4, 9
I have got to the point in my code where I have managed to find out which are the most common numbers. However, I don't want to print out the same number over and over again; example from above: Most common: 4, 9, 9, 4
What needs to be done?

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] input = new String[100];
    System.out.print("Input: ");
    input[0] = in.readLine();
    int size = 0;
    for (int i = 1; i < 100 && isNumeric(input[i-1]); i++) {
            System.out.print("Input: ");
            input[i] = in.readLine();
            size = size + 1;
    }
    /*for (int i = 0; i < size; i++) { //testing
        System.out.println(input[i]);
    }*/
    int numOccur;
    int[] occur = new int[size];
    for(int i = 0; i < size; i++) {
        numOccur = 0;
        for (int j = 0; j < size; j++) {
            if(input[i].equals(input[j])) {
                numOccur = numOccur + 1;
            }
        }
        occur[i] = numOccur;
        //System.out.println(numOccur); //testing
    }
    int maxOccur = 0;
    for(int i = 0; i < size; i++) {
        if(occur[i] > maxOccur) {
            maxOccur = occur[i];
        }
    }
    //System.out.println(maxOccur); //testing
    for (int i = 0; i < size && !numFound; i++) {
        if(occur[i] == maxOccur) {
           System.out.println(input[i]);
        }
    }

}

//checks if s is an in, true if it is an int
public static boolean isNumeric (String s) {
    try {
        Integer.parseInt(s);
        return true; //parse was successful
    } catch (NumberFormatException nfe) {
        return false;
    }
}

Found the solution!

String[] mostCommon = new String[size];
    int numMostCommon = 0;
    boolean numFound = false;
    for (int i = 0; i < size; i++) {
        int isDifferent = 0;
        if (occur[i] == maxOccur) {
            for (int j = 0; j < size; j++) {
                if (!(input[i].equals(mostCommon[j]))) {
                    isDifferent = isDifferent + 1;
                }
            }
            if (isDifferent == size) {
                mostCommon[numMostCommon] = input[i];
                numMostCommon = numMostCommon + 1;
            }
        }
    }
    for (int i = 0; i < numMostCommon - 1; i++) {
        System.out.print("Most common: " + mostCommon[i] + ", ");
    }
    System.out.println(mostCommon[numMostCommon - 1]);

Upvotes: 0

Views: 1410

Answers (4)

Beep beep
Beep beep

Reputation: 19151

What about something like this?

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    Map<string,int> numberLookup = new HashMap<string,int>();
    Boolean doContinue = true;
    while (doContinue)
    {
        System.out.print("Input: ");
        String input = in.readLine();
        if (isNumeric(input))
        {
            if (!numberLookup.containsKey(input))
                numberLookup.put(input,1);
            else
                numberLookup.put(input, numberLookup.get(input) + 1);
        }
        else
            doContinue = false;
    }

    maxOccur = numberLookup.values().max();
    System.out.print("These numbers were all entered " + maxOccur + " times:");
    Iterator it = numberLookup.entrySet().iterator();
    while (it.hasNext())
    {
        (Map.Entry)it.next();
        System.out.println(pairs.getKey());
    } 
}

Sorry, I'm a C# person and don't have a Java compiler on me, so this might need some tweaking.

Upvotes: 0

aquasan
aquasan

Reputation: 374

you could use the hash table for this to store the frequenceis as the limit is very less i.e. less than 100. pseudo code would be like:
vector<int> hash(101)
cin>>input
if(isnumeric(input))
hash[input]++
else{
max=max_element(hash.begin(),hash.end());
for(int i=0;i<100;i++)
if(hash[i]==max)
print i
}

Upvotes: 1

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

    Set<Integer> uniqueMaxOccur = new HashSet<Integer>();  
    for (int i = 0; i < size ; i++) {
        if(occur[i] == maxOccur) {
            //System.out.println(input[i]);
            uniqueMaxOccur.add(input[i]);
        }
    }

and display the values in the set

Upvotes: 0

dash1e
dash1e

Reputation: 7807

You can use a Set and store the values already printed.

Upvotes: 0

Related Questions