0

Please can anyone of you help me with a code that gives how many times each number is repeated in an array(frequency)? Thnx

28th Jan 2017, 10:52 PM
Dan
13 ответов
+ 1
Java, C# or C++ or LASAGNA??
28th Jan 2017, 11:28 PM
Garfield
Garfield - avatar
+ 1
I am bad in Java, but I will still explain how I could. Try to define a function that takes a character and the array. Declare a variable count to 0. Then use a for loop (I think they are available in Java) like for character in array. If the character is found, increment the count by one. In the end it should return count. After that use another for loop whoch says something like: for char in "abcdef...z" then it should call the function. Hope it helps. Can I have a lasagna please?😉
28th Jan 2017, 11:33 PM
Garfield
Garfield - avatar
+ 1
Probably easiest if you sort the array first (I used quick sort) and after that count the numbers. If you do it this way the output will even be in order. /* first call: left = 0 and right = input.length - 1 */ static void qsort(int input[], int left, int right){ if(right <= left+1){ return; } int t = input[right]; int w = left; for(int i = left; i < right; i++){ if(input[i] < t){ input[w] = input[w] + input[i] - (input[i] = input[w]); w++; } } input[w] = input[w] + input[right] - (input[right] = input[w]); qsort(input, left, w-1); qsort(input, w+1, right); } static void count_repeats(int input[]){ int t = 1; for(int i = 1; i < input.length; i++){ if(input[i] == input[i-1]){ t++; } else { System.out.println(input[i-1] + " is repeated " + t + " time(s)."); t = 1; } } System.out.println(input[input.length-1] + " is repeated " + t + " time(s)."); }
29th Jan 2017, 12:02 AM
Robobrine
Robobrine - avatar
0
hahaha java plz
28th Jan 2017, 11:29 PM
Dan
0
thnx...y not😜
28th Jan 2017, 11:45 PM
Dan
0
@Robobrine thnx for your willingness bro...but the thing is that we mustn't change the order,so i can't use qsort
29th Jan 2017, 12:11 AM
Dan
0
Even if you just sort a copy of the array? Well, then you could probably use a HashMap that stores the number and its frequency.
29th Jan 2017, 12:16 AM
Robobrine
Robobrine - avatar
0
well what is given as a hint is that u need to create other 2 vectors(1st one in the same size with our array) and there we put all our numbers but not repeated and in the second vector created(same size with how many places are filled in the previous vector) we will have sorted the value that each number in that position is repeated which will give our answer
29th Jan 2017, 12:31 AM
Dan
0
Wait, isn't this like the answer to your question? I mean it already tells you how to do it...
29th Jan 2017, 12:34 AM
Robobrine
Robobrine - avatar
0
well it just says that "for that task to be done u need to deal with 2 created vectors"... and the rest is what i kind of think but I'm not sure because I'm a beginner in coding
29th Jan 2017, 12:41 AM
Dan
0
What do you have so far? I mean you won't get such a task without some basic programming knowledge, so you must have something, right?
29th Jan 2017, 12:44 AM
Robobrine
Robobrine - avatar
0
well this how to create classes,methods and functions,nested if's loops and some recursive stuff... and now we just took vectors and matrices and this task showed up
29th Jan 2017, 12:50 AM
Dan
29th Jan 2017, 12:54 AM
Robobrine
Robobrine - avatar