๐Ÿ”’ Closed Occurring numbers! ๐Ÿ‘

Status
Not open for further replies.

meta29

Enthusiast
Suppose gustong mong malaman ang occurrence ng number sa array without using HashMap pwedi mong i copy ang array mo sa panibagong array and now mayroon ka ng dalawang array ๐Ÿ˜… so yung copied array gagamiting reference halimbawa


Original = 1 3 4 5 6 8 9
Copied = 1 3 4 5 6 8 9


And sympre gagawa ka ng loop,


is Copied [1] equals to Original [1]
is Copied [1] equals to Original [3]
is Copied [1] equals to Original [4]


So on and so forth. Paano pag 1000 na ang length ng array ๐Ÿ˜…mas mabagal?


ganito nalang.



[] Original = 1 3 4 5 6 8 9
[] Copied = new int [ Original.length ]


for (int i = 0....


int temp = Original [ i ] ;
Copied[temp] += 1;
}
ngayon hindi na sya mag co compare sa lahat nag value ni Original and Copied so maka save ng oras ๐Ÿ‘


paano kung may number tayong mas mataas
Sa length ni Original example 99


[] Original = 1 3 4 5 6 8 99
[] Copied = new int [ Original length]


Coppied [99] += 1
wala namn tayong Copied na may index na 99!
error na! Out of bounds tsk tsk tsk.
dahil naka depende si Copied sa length ni Original


so kailangan nalang natin i sort si Original from least to greatest and kunin si last value ni array which is the highest. Tapos compare natin sa length ni array kapag mas mataas si length kaysa sa highest value ni Original, si length ang magiging reference natin sa value ni Copied. Vice versa.


and now problma nalang natin ang mga numbers na hindi kabilang sa value ni Original array na dada anan ni copied array. Like 98,97 etc.


pwedi naman gawan ng method para mag jump or i skip sa mga inappropriate numbers.
Pero for now i hide nalng natin using if statement.


And whoossh magic! Mayroong na tayong algorithm para mahanap ang occurrences ng numbers!


Pero parang mas humaba ata. Dahil ky sort so parang same lang ng nauna hahaha gg.


and test natin sa random numbers tapos ang array length ay 10000.


Algorithm ba ang tamang term?


Sana nakatulong hehehe.


Not dabest pero ok na.


Aral
Aral.
Aral.

Code:
package Temp;
class Sorter {

  int sortMe(int [] ray){
    int returner = 0;
   for(int i = 0; i < ray.length; i++){
     for(int j = i; j < ray.length; j++){
      
        if(ray[i] > ray[j]){
          
          int temp = ray[i];
          ray[i] = ray[j];
          ray[j] = temp;
        }
      
  
    }
  }
    
   if(ray.length >= ray[ray.length - 1]){
     returner = ray.length;
   }else {
      returner = ray[ray.length - 1];
    }

  return returner;
}

}


public class insertionSort {
public static void main (String []args) {

  Sorter get = new Sorter ();

  int [] target = new int [100];

  for (int i = 0; i < 100; i++){
  
   target [i] = (int)(Math.random()*100);

  }

  int le = get.sortMe(target);
  int [] counter = new int [le];


for(int i = 0; i < target.length; i++){
  
     int temp = target[i];
    
     counter[temp] += 1;

  }


for(int i = 0; i < target.length; i++){
   if (counter[i]!= 0){
   System.out.print(i + " has Frequency of: " );
   System.out.println(counter[i]);

   }
  
   }
  
   for(int e : target){
     System.out.println(e);
   }







}
}


nshot_20200415_200603_com.duy.compiler.javanide.md.webp nshot_20200415_200610_com.duy.compiler.javanide.md.webp

Alert! Pwedi ka gumamit ng bult in sort sa java! Gumamit lang ako ng selection para ma practice na din sa Sorting. Salamat sa pagbabasa[/I]
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 498
    Views
  • 2
    Participants
Last reply from:
meta29

Online now

Members online
1,039
Guests online
695
Total visitors
1,734

Forum statistics

Threads
2,276,966
Posts
28,973,387
Members
1,229,669
Latest member
zeeeee345
Back
Top