Dada una array ordenada arr[] de tamaño N, la tarea es imprimir todos los elementos únicos en la array .
Se dice que un elemento de array es único si la frecuencia de ese elemento en la array es 1 .
Ejemplos:
Entrada: arr[ ] = {1, 1, 2, 2, 3, 4, 5, 5}
Salida: 3 4
Explicación: Dado que 1, 2, 5 aparecen más de una vez en la array, los distintos elementos son 3 y 4.Entrada: arr[ ] = {1, 2, 3, 3, 3, 4, 5, 6}
Salida: 1 2 4 5 6
Enfoque: El enfoque más simple para resolver el problema es atravesar la array arr[] e imprimir solo aquellos elementos cuya frecuencia es 1 . Siga los pasos a continuación para resolver el problema:
- Iterar sobre el arreglo arr[] e inicializar una variable, digamos cnt = 0, para contar la frecuencia del elemento del arreglo actual.
- Dado que la array ya está ordenada , compruebe si el elemento actual es el mismo que el elemento anterior. Si se determina que es cierto, actualice cnt += 1 .
- De lo contrario, si cnt = 1, imprima el elemento. De lo contrario, continúa .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program for the above approach #include <bits/stdc++.h> 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
<script> // JavaScript Program for the above approach // Function to print all unique // elements present in a sorted array function RemoveDuplicates(arr, n) { let i = 0; // Traverse the array while (i < n) { let cur = arr[i]; // Stores frequency of // the current element let 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) { document.write(cur + " "); } } } // Driver Code // Given Input let arr = [1, 3, 3, 5, 5, 6, 10]; let N = 7; // Function Call RemoveDuplicates(arr, N); // This code is contributed by Potta Lokesh </script>
1 6 10
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por hrithikgarg03188 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA