伪代码查找列表中多次出现的数字

发布于 2024-12-28 05:06:03 字数 331 浏览 0 评论 0原文

我有一个像 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


}

I have a series like 1,2,199,100,8,100,199,1001,5,9 and I got to write a pseudo code to find out the numbers which appear more then once in the above list. I can clearly see that 199 and 100 appear twice in the list and that should be the answer but how should I write the pseudocode for it?
My logic is something like this:

   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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谈情不如逗狗 2025-01-04 05:06:03

使用快速排序或合并排序 (n log n) 对列表进行排序,然后对列表进行一次遍历,将当前数字与之前的 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"

Sort the list with quick sort or merge sort (n log n) then do a single pass over the list comparing the current number to the previous O(n). If the previous number equals the current then you have a duplicate.

EDIT:

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"
盗琴音 2025-01-04 05:06:03

通过exists() 检查,这看起来与冒泡排序具有相同的性能。如果您对数组进行排序(使用更快的排序),然后再执行一次额外的操作来识别重复项,那么可能会更快。
如果我正确理解你的伪代码,它似乎有一个错误。难道不应该更像是:

for(int i = 0;i< 9; i++){
 if x[i] exists in y
     j.push(x[i]);
 else
    y.push(x[i]);    

}

With the exists() check this looks like it would have the same performance as a bubble sort. It would probably be faster if you sorted the array (with a faster sort) then did one extra pass to identify the dupes.
If I understand your pseudo code correctly it seems to have a bug. Shouldn't it be more like:

for(int i = 0;i< 9; i++){
 if x[i] exists in y
     j.push(x[i]);
 else
    y.push(x[i]);    

}
江湖正好 2025-01-04 05:06:03
// loop through list of numbers
    // count apperences in list
    // if appearences > 1
            // remove all instances, add to results list

// print the results list -- this will have all numbers that appear more than once.
// loop through list of numbers
    // count apperences in list
    // if appearences > 1
            // remove all instances, add to results list

// print the results list -- this will have all numbers that appear more than once.
我要还你自由 2025-01-04 05:06:03

假设您仅限于使用基本类型而不能使用 java.util.Collections,您可以像这样工作:

For each value in `x`
  Grab that value
  If the list of duplicates does not contain that value
    See if that value reoccurs at a later index in `x`
      If it does, add it to the list of duplicates

下面是翻译为 Java 的伪代码:

private void example() {
    int [] x = new int [] {1,2,199,100,8,100,199,1001,5,9, 199};
    int [] duplicates = new int[x.length];

    for (int i = 0; i < x.length; i++) {
      int key = x[i];
      if (!contains(duplicates, key)) {
        // then check if this number is a duplicate
        for (int j = i+1; j < x.length-1; j++) {
          // start at index i+1 (no sense checking same index as i, or before i, since those are alreayd checked
          // and stop at x.length-1 since we don't want an array out of bounds exception
          if (x[j] == key) {
            // then we have a duplicate, add to the list of duplicates
            duplicates[i] = key;
          }
        }
      }
    }

    for (int n = 0; n < duplicates.length; n++) {
      if (duplicates[n] != 0) {
        System.out.println(duplicates[n]);
      }
    }
}

  private boolean contains(int [] array, int key) {
    for (int i = 0; i < array.length; i++) {
      if (array[i] == key) {
        return true;
      }
    }
    return false;
  }

Assuming you're restricted to only using primitive types rather than being able to use java.util.Collections, you could work it like this:

For each value in `x`
  Grab that value
  If the list of duplicates does not contain that value
    See if that value reoccurs at a later index in `x`
      If it does, add it to the list of duplicates

Here's the pseudocode translated to Java:

private void example() {
    int [] x = new int [] {1,2,199,100,8,100,199,1001,5,9, 199};
    int [] duplicates = new int[x.length];

    for (int i = 0; i < x.length; i++) {
      int key = x[i];
      if (!contains(duplicates, key)) {
        // then check if this number is a duplicate
        for (int j = i+1; j < x.length-1; j++) {
          // start at index i+1 (no sense checking same index as i, or before i, since those are alreayd checked
          // and stop at x.length-1 since we don't want an array out of bounds exception
          if (x[j] == key) {
            // then we have a duplicate, add to the list of duplicates
            duplicates[i] = key;
          }
        }
      }
    }

    for (int n = 0; n < duplicates.length; n++) {
      if (duplicates[n] != 0) {
        System.out.println(duplicates[n]);
      }
    }
}

  private boolean contains(int [] array, int key) {
    for (int i = 0; i < array.length; i++) {
      if (array[i] == key) {
        return true;
      }
    }
    return false;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文