📜  打印排序数组中存在的所有唯一元素

📅  最后修改于: 2022-05-13 01:56:10.652000             🧑  作者: Mango

打印排序数组中存在的所有唯一元素

给定一个大小为N的排序数组arr[] ,任务是打印数组中的所有唯一元素。

例子:

方法:解决问题的最简单方法是遍历数组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


Python3
# 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.


Javascript


输出
1 6 10 

时间复杂度: O(N)
辅助空间: O(1)