It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
我有一个类似于1,2,199,100,8,100,199,1001,5,9的序列,我必须编写一个伪代码来找出出现在上面列表中不止一次的数字我可以清楚地看到199和100在列表中出现了两次,这应该是答案,但我应该如何编写伪代码呢?
我的逻辑是这样的:

   array x = {1,2,199,100,8,100,199,1001,5,9}
   array y
   array j
for(int i = 0;i< 9; i++){
 if x[i] exists in y
     j[i] = y
 else
    y[i] = x


}

最佳答案

使用快速排序或合并排序(n logn)对列表进行排序,然后在列表上执行一次传递,将当前编号与前一个o(n)进行比较。如果前一个数字等于当前数字,则有一个重复的数字。
编辑:

Array array = {1,2,199,100,8,100,199,1001,5,9}
Array sorted = sort(array)
for (int i=1; i<sorted.length; i++)
    int p = sorted[i-1]
    int c = sorted[i]
    if (p==c) print "duplicate"

关于java - 伪代码以查找列表中出现多次的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8914784/

10-16 18:00
查看更多