-2

Logic please.. With just using for or while loop...
Example:

int a[ ]  = {2,3,2,3,4,4,5,1,3};

Output :

2 occurred 2 time
3 occurred 3 time
4 occurred 2 time
5 occurred 1 time
1 occurred 1 time
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    You'd probably be better served by figuring this out yourself, but to get you started... why don't you look at each number in turn and save a running total of how many times you've seen each number? Lots of data structures to help you with this... look for a Map structure in your language of choice (eg: http://docs.oracle.com/javase/7/docs/api/java/util/Map.html) – Teddy Ward Jul 08 '15 at 18:56
  • If you use C#, then you can do group in LINQ. – rnofenko Jul 08 '15 at 20:00
  • Possible duplicate: http://stackoverflow.com/questions/8098601/java-count-occurrence-of-each-item-in-an-array – Fernando Martín Besteiro Aug 03 '15 at 17:34
  • Effort on your part please... – takendarkk Aug 03 '15 at 17:43

1 Answers1

1

The logic for this task could be as follow:

For each element in array a

  1. check if element was previously used (may create array b to add unique element)
    1.1 If it was used continue, otherwise add to array b

  2. initialize counter and currentIndex variables

  3. Search and count number of occurrences from the currentIndex, while currentIndex value is not -1

  4. Print the element and its counter

MaxZoom
  • 7,619
  • 5
  • 28
  • 44