+ 2

array

public class MyClass { public static void main(String args[]) { int []n ={1,2,5,1,2,3,4,2,4,1}; int [] occ = new int[6]; for(int i = 0; i <n.length; i++) ++ occ [n[i]]; System.out.println(occ[1]); System.out.println(occ[4]); } }; // still dont know why it gives such a result ; I mean 3 and 2

7th Nov 2018, 4:00 AM
Gustave A C/D C ☢️ 🛸🛸🛸
Gustave A C/D C ☢️ 🛸🛸🛸 - avatar
2 Respostas
+ 3
Java initializes occ contents to 0. The rest is about handtracing. ++occ[n[0]] ++occ[1] occ[1] is now 1. ++occ[n[1]] ++occ[2] occ[2] is now 1 ... (I'll leave the rest to you) An easy way would be to inspect the contents of n. We can see that 1 occurs 3 times in n, and we know that the contents of n will be iterated through and used as the index for occ. So, occ[1] would be 3. In the end, occ[1] and occ[4] is 3 and 2 respectively
7th Nov 2018, 4:34 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
|So its like an Histogram ; captures the amount of ocurrences??; this is more complex than I thought.
7th Nov 2018, 4:50 PM
Gustave A C/D C ☢️ 🛸🛸🛸
Gustave A C/D C ☢️ 🛸🛸🛸 - avatar