原文:

给定一个大小为 n、arr[] ,任务是打印中所有唯一的元素。

如果数组中某个元素的频率为 1 ,则称该元素为唯一

示例:

输入: arr[ ] = {1,1,2,2,3,4,5,5} 输出: 3 4 解释:由于 1,2,5 在数组中出现不止一次,所以不同的元素是 3 和 4。

输入: arr[ ] = {1,2,3,3,3,4,5,6} 输出: 1 2 4 5 6

方法:解决问题最简单的方法就是遍历数组arr【】只打印那些频率为 1 的元素。按照以下步骤解决问题:

  • arr[] 并初始化一个变量,比如 cnt = 0,来计算当前数组元素的频率。
  • 由于已经是,检查当前元素是否与前一个元素相同。如果发现是真的,那么更新 cnt = 1
  • 否则,如果 cnt = 1,打印元素。否则,继续

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to print all unique
// elements present in a sorted array
void removeduplicates(int arr[], int n)
{
    int i = 0;
    // traverse the array
    while (i < n) {
        int cur = arr[i];
        // stores frequency of
        // the current element
        int cnt = 0;
        // iterate until end of the
        // array is reached or current
        // element is not the same as the
        // previous element
        while (i < n and cur == arr[i]) {
            cnt  ;
            i  ;
        }
        // if current element is unique
        if (cnt == 1) {
            cout << cur << " ";
        }
    }
}
// driver code
int main()
{
    // given input
    int arr[] = { 1, 3, 3, 5, 5, 6, 10 };
    int n = 7;
    // function call
    removeduplicates(arr, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.io.*;
class gfg
{
  // function to print all unique
  // elements present in a sorted array
  static void removeduplicates(int arr[], int n)
  {
    int i = 0;
    // traverse the array
    while (i < n) {
      int cur = arr[i];
      // stores frequency of
      // the current element
      int cnt = 0;
      // iterate until end of the
      // array is reached or current
      // element is not the same as the
      // previous element
      while (i < n && cur == arr[i]) {
        cnt  ;
        i  ;
      }
      // if current element is unique
      if (cnt == 1) {
        system.out.print(cur  " ");
      }
    }
  }
  // driver code
  public static void main (string[] args)
  {
    // given input
    int arr[] = { 1, 3, 3, 5, 5, 6, 10 };
    int n = 7;
    // function call
    removeduplicates(arr, n);
  }
}
// this code is contributed by potta lokesh

python 3

# function to print all unique
# elements present in a sorted array
def removeduplicates(arr, n):
    i = 0
    while i < n:
        cur = arr[i]
        # stores frequency of
        # the current element
        cnt = 0
        # iterate until end of the
        # array is reached or current
        # element is not the same as the
        # previous element
        while i < n and cur == arr[i]:
            cnt  = 1
            i  = 1
        if cnt == 1:
            print(cur, end=" ")
# driver code
if __name__ == "__main__":
    # given input
    arr = [1, 3, 3, 5, 5, 6, 10]
    n = 7
    # function call
    removeduplicates(arr, n)
# this code is contributed by kushagra bansal

c

// c# program for the above approach
using system;
class gfg {
    // function to print all unique
    // elements present in a sorted array
    static void removeduplicates(int[] arr, int n)
    {
        int i = 0;
        // traverse the array
        while (i < n) {
            int cur = arr[i];
            // stores frequency of
            // the current element
            int cnt = 0;
            // iterate until end of the
            // array is reached or current
            // element is not the same as the
            // previous element
            while (i < n && cur == arr[i]) {
                cnt  ;
                i  ;
            }
            // if current element is unique
            if (cnt == 1) {
                console.write(cur   " ");
            }
        }
    }
    // driver code
    public static void main()
    {
        // given input
        int[] arr = { 1, 3, 3, 5, 5, 6, 10 };
        int n = 7;
        // function call
        removeduplicates(arr, n);
    }
}
// this code is contributed by rishavmahato348.

java 描述语言


output

1 6 10 

时间复杂度:o(n) t5辅助空间:** o(1)