Algorithm questions in an Alibaba telephone interview

Source: Internet
Author: User

Telephone Interview algorithm question: Find the elements with the most repeated entries in the array and print

 

The problem is not difficult. You can give a better solution.

 

 

Java code
  1. Import java. util. hashmap;
  2. Import java. util. iterator;
  3. Import java. util. Map. entry;
  4. Import commons. algorithm. Sort. quicksort;
  5. /**
  6. * Find the most repeated elements in the array and print them.
  7. *
  8. */
  9. Public class problem_3 {
  10. // Search for O (N * log2 (n) + n) cyclically after fast sorting)
  11. Public static void find1 (INT [] ARR ){
  12. Quicksort. Sort (ARR );
  13. Int max = arr [0];
  14. Int pre = 1;
  15. Int now = 1;
  16. For (INT I = 0; I <(ARR. Length-1); I ++ ){
  17. If (ARR [I] = arr [I + 1])
  18. Now ++;
  19. Else {
  20. If (now> = pre ){
  21. Pre = now;
  22. Now = 1;
  23. Max = arr [I];
  24. }
  25. }
  26. }
  27. }
  28. // Nested loop query O (N * n)
  29. Public static void find2 (INT [] ARR ){
  30. Int pre = 0;
  31. Int max = arr [0];
  32. For (INT I = 0; I <arr. length; I ++ ){
  33. Int now = 0;
  34. For (Int J = 0; j <arr. length; j ++ ){
  35. If (ARR [I] = arr [J]) {
  36. Now ++;
  37. }
  38. }
  39. If (now> = pre ){
  40. Max = arr [I];
  41. Pre = now;
  42. }
  43. }
  44. }
  45. // Hash
  46. Public static void find3 (INT [] ARR ){
  47. Hashmap <integer, integer> Hm = new hashmap <integer, integer> ();
  48. For (INT I = 0; I <arr. length; I ++ ){
  49. If (Hm. containskey (ARR [I]) {
  50. Int COUNT = hm. Get (ARR [I]);
  51. Hm. Put (ARR [I], ++ count );
  52. } Else {
  53. Hm. Put (ARR [I], 1 );
  54. }
  55. }
  56. Iterator <entry <integer, integer> it = hm. entryset (). iterator ();
  57. Int pre = 0;
  58. Int max = arr [0];
  59. While (it. hasnext ()){
  60. Entry <integer, integer> en = it. Next ();
  61. Int key = en. getkey ();
  62. Int val = en. getvalue ();
  63. If (Val> pre ){
  64. Pre = val;
  65. Max = key;
  66. }
  67. }
  68. }
  69. Public static void main (string ARGs []) {
  70. // There are more than 800 duplicate elements in the amount of data, which are: 46 3680 195
  71. Int arr2 [] = {0, 1, 2 ,.....
  72. , 0, 1, 2, 3, 6, 7, 8, 9 };
  73. // The amount of data is 800. There are few duplicate elements, and the search time is 82 3727 360.
  74. Int arr [] = {, 6 ......
  75. , 730,731, 52, 53, 3,794, 95,796,797,798,799 };
  76. Long start, end;
  77. Start = system. currenttimemillis ();
  78. For (INT I = 0; I <1000; I ++) find1 (ARR );
  79. End = system. currenttimemillis ();
  80. System. Out. println (end-Start );
  81. Start = system. currenttimemillis ();
  82. For (INT I = 0; I <1000; I ++) find2 (ARR );
  83. End = system. currenttimemillis ();
  84. System. Out. println (end-Start );
  85. Start = system. currenttimemillis ();
  86. For (INT I = 0; I <1000; I ++) find3 (ARR );
  87. End = system. currenttimemillis ();
  88. System. Out. println (end-Start );
  89. }
  90. }

Reprinted from: http://leves.javaeye.com/blog/777508

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.