Find Occurrences in Array

Abdul Majeed

I Have to find the occurrences of every element in array. So far My code is this

void Occurrences()
{
    int numers[10], count = 0, i;
    for (i = 0; i < 10; i++)
    {
        cout << "Enter Number";
        cin >> numers[i];
    }
    for (i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (numers[i] == numers[j])
            {
                count++;
            }
        }
        cout << numers[i] << " is Occur " << count << " Time in Array" << endl;
        count = 0;
    }
}
int main()
{
    Occurrences();
}

Output is coming multiply same numbers i.e If I entered six 1 and 4 2's. Output is

1 is occur 6 time in array.
1 is occur 6 time in array.
1 is occur 6 time in array.
1 is occur 6 time in array.
1 is occur 6 time in array.
1 is occur 6 time in array.
2 is occur 4 time in array.
2 is occur 4 time in array.
2 is occur 4 time in array.
2 is occur 4 time in array.

But I want output like this:

1 is occur 6 time in array.
2 is occur 4 time in array.

How do I do this?

Barry

Since you tagged this C++11, I would use std::unordered_map:

void Occurrences()
{
    std::unordered_map<int, int> occurrences;

    // enter 10 numbers
    for (int i = 0; i < 10; ++i) {
        cout << "Enter Number";
        int x;
        cin >> x;
        ++occurrences[x]; // increment the count for x
    }

    // print results
    for (const auto& pr : occurrences) {
        std::cout << pr.first << " appears " << pr.second << " times." << std::endl;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find Occurrences in Array

From Dev

Find occurrences of string on array - JQuery

From Dev

Iterating through array to find occurrences of value

From Java

How to find index of all occurrences of element in array?

From Dev

Find out number of array occurrences in php

From Dev

ruby: find item with most occurrences in array, if there is

From Dev

Find all occurrences (letters) in array (word)

From Dev

Occurrences in Array

From Dev

Numpy find number of occurrences in a 2D array

From Dev

RethinkDB REQL Query to find number occurrences of distinct values in an array

From Dev

How to find occurrences of the largest integer in C without array?

From Dev

Find the occurrences (count) of all elements at all positions in an array in Postgres?

From Dev

Find the occurrences (count) of all elements at all positions in an array in Postgres?

From Dev

Java - Use binarySearch to find # of occurrences of certain element in array, without If

From Dev

RethinkDB REQL Query to find number occurrences of distinct values in an array

From Dev

Numpy find number of occurrences in a 2D array

From Dev

Find the 3 values with highest occurrences in a javascript array. I only need 3 most occurrences

From Dev

Find occurrences of objects in list

From Dev

Find occurrences of a word in a string

From Dev

Counting occurrences of integers in an array

From Dev

Counting Occurrences With An Array - Java

From Dev

Counting occurrences inside an array

From Dev

Count occurrences of array in string

From Dev

Return occurrences in multidimensional array

From Dev

How to find index of all occurrences of an element in array (Ramda.js way)?

From Dev

How to find index of all occurrences of an element in array (Ramda.js way)?

From Dev

User selects an item and it searches through an array to find and count all the occurrences of the items that were selected

From Dev

Find total number of occurrences of a substring

From Dev

Find a number of occurrences of character in string

Related Related

  1. 1

    Find Occurrences in Array

  2. 2

    Find occurrences of string on array - JQuery

  3. 3

    Iterating through array to find occurrences of value

  4. 4

    How to find index of all occurrences of element in array?

  5. 5

    Find out number of array occurrences in php

  6. 6

    ruby: find item with most occurrences in array, if there is

  7. 7

    Find all occurrences (letters) in array (word)

  8. 8

    Occurrences in Array

  9. 9

    Numpy find number of occurrences in a 2D array

  10. 10

    RethinkDB REQL Query to find number occurrences of distinct values in an array

  11. 11

    How to find occurrences of the largest integer in C without array?

  12. 12

    Find the occurrences (count) of all elements at all positions in an array in Postgres?

  13. 13

    Find the occurrences (count) of all elements at all positions in an array in Postgres?

  14. 14

    Java - Use binarySearch to find # of occurrences of certain element in array, without If

  15. 15

    RethinkDB REQL Query to find number occurrences of distinct values in an array

  16. 16

    Numpy find number of occurrences in a 2D array

  17. 17

    Find the 3 values with highest occurrences in a javascript array. I only need 3 most occurrences

  18. 18

    Find occurrences of objects in list

  19. 19

    Find occurrences of a word in a string

  20. 20

    Counting occurrences of integers in an array

  21. 21

    Counting Occurrences With An Array - Java

  22. 22

    Counting occurrences inside an array

  23. 23

    Count occurrences of array in string

  24. 24

    Return occurrences in multidimensional array

  25. 25

    How to find index of all occurrences of an element in array (Ramda.js way)?

  26. 26

    How to find index of all occurrences of an element in array (Ramda.js way)?

  27. 27

    User selects an item and it searches through an array to find and count all the occurrences of the items that were selected

  28. 28

    Find total number of occurrences of a substring

  29. 29

    Find a number of occurrences of character in string

HotTag

Archive