📌  相关文章
📜  打印给定整数数组的所有不同元素

📅  最后修改于: 2021-10-27 07:53:37             🧑  作者: Mango

给定一个整数数组,打印数组中所有不同的元素。给定的数组可能包含重复项,并且输出应该只打印每个元素一次。给定的数组未排序。
例子:

Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}
Output: 12, 10, 9, 45, 2

Input: arr[] = {1, 2, 3, 4, 5}
Output: 1, 2, 3, 4, 5

Input: arr[] = {1, 1, 1, 1, 1}
Output: 1

一个简单的解决方案是使用 twp 嵌套循环。外循环从最左边的元素开始一个一个地选取一个元素。内循环检查元素是否存在于它的左侧。如果存在,则忽略该元素,否则打印该元素。下面是简单算法的实现。

C++
// C++ program to print all distinct elements in a given array
#include 
using namespace std;
  
void printDistinct(int arr[], int n)
{
    // Pick all elements one by one
    for (int i=0; i


Java
// Java program to print all distinct
// elements in a given array
import java.io.*;
  
class GFG {
  
    static void printDistinct(int arr[], int n)
    {
        // Pick all elements one by one
        for (int i = 0; i < n; i++)
        {
            // Check if the picked element 
            // is already printed
            int j;
            for (j = 0; j < i; j++)
            if (arr[i] == arr[j])
                break;
      
            // If not printed earlier, 
            // then print it
            if (i == j)
            System.out.print( arr[i] + " ");
        }
    }
      
    // Driver program
    public static void main (String[] args) 
    {
        int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
        int n = arr.length;
        printDistinct(arr, n);
  
    }
}
  
// This code is contributed by vt_m


Python3
# python program to print all distinct
# elements in a given array
  
def printDistinct(arr, n):
  
    # Pick all elements one by one
    for i in range(0, n):
  
        # Check if the picked element 
        # is already printed
        d = 0
        for j in range(0, i):
            if (arr[i] == arr[j]):
                d = 1
                break
  
        # If not printed earlier,
        # then print it
        if (d == 0):
            print(arr[i])
      
# Driver program to test above function
arr = [6, 10, 5, 4, 9, 120, 4, 6, 10]
n = len(arr)
printDistinct(arr, n)
  
# This code is contributed by Sam007.


C#
// C# program to print all distinct
// elements in a given array
using System;
  
class GFG {
  
    static void printDistinct(int []arr, int n)
    {
          
        // Pick all elements one by one
        for (int i = 0; i < n; i++)
        {
              
            // Check if the picked element 
            // is already printed
            int j;
            for (j = 0; j < i; j++)
                if (arr[i] == arr[j])
                     break;
      
            // If not printed earlier, 
            // then print it
            if (i == j)
            Console.Write(arr[i] + " ");
        }
    }
      
    // Driver program
    public static void Main () 
    {
        int []arr = {6, 10, 5, 4, 9, 120,
                                  4, 6, 10};
        int n = arr.Length;
          
        printDistinct(arr, n);
  
    }
}
  
// This code is contributed by Sam007.


PHP


Javascript


C++
// C++ program to print all distinct elements in a given array
#include 
using namespace std;
  
void printDistinct(int arr[], int n)
{
    // First sort the array so that all occurrences become consecutive
    sort(arr, arr + n);
  
    // Traverse the sorted array
    for (int i=0; i


Java
// Java program to print all distinct 
// elements in a given array
import java.io.*;
import java .util.*;
  
class GFG 
{
    static void printDistinct(int arr[], int n)
    {
        // First sort the array so that 
        // all occurrences become consecutive
        Arrays.sort(arr);
      
        // Traverse the sorted array
        for (int i = 0; i < n; i++)
        {
            // Move the index ahead while 
            // there are duplicates
            while (i < n - 1 && arr[i] == arr[i + 1])
                i++;
      
            // print last occurrence of 
            // the current element
            System.out.print(arr[i] +" ");
        }
    }
      
    // Driver program 
    public static void main (String[] args) 
    {
        int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
        int n = arr.length;
        printDistinct(arr, n);
  
    }
}
  
// This code is contributed by vt_m


Python3
# Python program to print all distinct 
# elements in a given array
  
def printDistinct(arr, n):
      
    # First sort the array so that 
    # all occurrences become consecutive
    arr.sort();
  
    # Traverse the sorted array
    for i in range(n):
          
        # Move the index ahead while there are duplicates
        if(i < n-1 and arr[i] == arr[i+1]):
            while (i < n-1 and (arr[i] == arr[i+1])):
                i+=1;
              
  
        # print last occurrence of the current element
        else:
            print(arr[i], end=" ");
  
# Driver code
arr = [6, 10, 5, 4, 9, 120, 4, 6, 10];
n = len(arr);
printDistinct(arr, n);
  
# This code has been contributed by 29AjayKumar


C#
// C# program to print all distinct 
// elements in a given array
using System;
  
class GFG {
  
    static void printDistinct(int []arr, int n)
    {
          
        // First sort the array so that 
        // all occurrences become consecutive
        Array.Sort(arr);
      
        // Traverse the sorted array
        for (int i = 0; i < n; i++)
        {
              
            // Move the index ahead while 
            // there are duplicates
            while (i < n - 1 && arr[i] == arr[i + 1])
                i++;
      
            // print last occurrence of 
            // the current element
            Console.Write(arr[i] + " ");
        }
    }
      
    // Driver program 
    public static void Main () 
    {
        int []arr = {6, 10, 5, 4, 9, 120, 4, 6, 10};
        int n = arr.Length;
          
        printDistinct(arr, n);
    }
}
  
// This code is contributed by Sam007.


PHP


Javascript


C++
/* CPP program to print all distinct elements 
   of a given array */
#include
using namespace std;
  
// This function prints all distinct elements
void printDistinct(int arr[],int n)
{
    // Creates an empty hashset
    unordered_set s;
  
    // Traverse the input array
    for (int i=0; i


Java
/* Java program to print all distinct elements of a given array */
import java.util.*;
  
class Main
{
    // This function prints all distinct elements
    static void printDistinct(int arr[])
    {
        // Creates an empty hashset
        HashSet set = new HashSet<>();
  
        // Traverse the input array
        for (int i=0; i


Python3
# Python3 program to print all distinct elements 
# of a given array 
  
# This function prints all distinct elements
def printDistinct(arr, n):
      
    # Creates an empty hashset
    s = dict();
  
    # Traverse the input array
    for i in range(n):
          
        # If not present, then put it in
        # hashtable and print it
        if (arr[i] not in s.keys()):
            s[arr[i]] = arr[i];
            print(arr[i], end = " ");
   
# Driver Code
arr = [10, 5, 3, 4, 3, 5, 6];
n = 7;
printDistinct(arr, n);
  
# This code is contributed by Princi Singh


C#
// C# program to print all distinct
// elements of a given array 
using System;
using System.Collections.Generic;
  
class GFG
{
// This function prints all 
// distinct elements 
public static void printDistinct(int[] arr)
{
    // Creates an empty hashset 
    HashSet set = new HashSet();
  
    // Traverse the input array 
    for (int i = 0; i < arr.Length; i++)
    {
        // If not present, then put it 
        // in hashtable and print it 
        if (!set.Contains(arr[i]))
        {
            set.Add(arr[i]);
            Console.Write(arr[i] + " ");
        }
    }
}
  
// Driver Code
public static void Main(string[] args)
{
    int[] arr = new int[] {10, 5, 3, 4, 3, 5, 6};
    printDistinct(arr);
}
}
  
// This code is contributed by Shrikant13


Javascript


Java
import java.util.HashMap;
public class UniqueInArray2 {
  
    public static void main(String args[])
  
    {
        int ar[] = { 10, 5, 3, 4, 3, 5, 6 };
        HashMap hm = new HashMap();
        for (int i = 0; i < ar.length; i++) {
            hm.put(ar[i], i);
        }
        // Using hm.keySet() to print output 
        // reduces time complexity. - Lokesh
        System.out.println(hm.keySet());
  
    }
  
}


C#
// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
public class UniqueInArray2 
{
  
    public static void Main(String []args)
  
    {
        int []ar = { 10, 5, 3, 4, 3, 5, 6 };
        Dictionary hm = new Dictionary();
        for (int i = 0; i < ar.Length; i++)
        {
            if(hm.ContainsKey(ar[i]))
                hm.Remove(ar[i]);
            hm.Add(ar[i], i);
        }
  
        // Using hm.Keys to print output 
        // reduces time complexity. - Lokesh
        var v = hm.Keys;
        foreach(int a in v)
            Console.Write(a+" ");
  
    }
  
}
  
/* This code contributed by PrinciRaj1992 */


输出:

6 10 5 4 9 120

上述解决方案的时间复杂度为 O(n 2 )。我们可以使用排序在 O(nLogn) 时间内解决问题。这个想法很简单,首先对数组进行排序,使每个元素的所有出现都变得连续。一旦出现连续,我们就可以遍历已排序的数组并在 O(n) 时间内打印不同的元素。下面是这个想法的实现。

C++

// C++ program to print all distinct elements in a given array
#include 
using namespace std;
  
void printDistinct(int arr[], int n)
{
    // First sort the array so that all occurrences become consecutive
    sort(arr, arr + n);
  
    // Traverse the sorted array
    for (int i=0; i

Java

// Java program to print all distinct 
// elements in a given array
import java.io.*;
import java .util.*;
  
class GFG 
{
    static void printDistinct(int arr[], int n)
    {
        // First sort the array so that 
        // all occurrences become consecutive
        Arrays.sort(arr);
      
        // Traverse the sorted array
        for (int i = 0; i < n; i++)
        {
            // Move the index ahead while 
            // there are duplicates
            while (i < n - 1 && arr[i] == arr[i + 1])
                i++;
      
            // print last occurrence of 
            // the current element
            System.out.print(arr[i] +" ");
        }
    }
      
    // Driver program 
    public static void main (String[] args) 
    {
        int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
        int n = arr.length;
        printDistinct(arr, n);
  
    }
}
  
// This code is contributed by vt_m

蟒蛇3

# Python program to print all distinct 
# elements in a given array
  
def printDistinct(arr, n):
      
    # First sort the array so that 
    # all occurrences become consecutive
    arr.sort();
  
    # Traverse the sorted array
    for i in range(n):
          
        # Move the index ahead while there are duplicates
        if(i < n-1 and arr[i] == arr[i+1]):
            while (i < n-1 and (arr[i] == arr[i+1])):
                i+=1;
              
  
        # print last occurrence of the current element
        else:
            print(arr[i], end=" ");
  
# Driver code
arr = [6, 10, 5, 4, 9, 120, 4, 6, 10];
n = len(arr);
printDistinct(arr, n);
  
# This code has been contributed by 29AjayKumar

C#

// C# program to print all distinct 
// elements in a given array
using System;
  
class GFG {
  
    static void printDistinct(int []arr, int n)
    {
          
        // First sort the array so that 
        // all occurrences become consecutive
        Array.Sort(arr);
      
        // Traverse the sorted array
        for (int i = 0; i < n; i++)
        {
              
            // Move the index ahead while 
            // there are duplicates
            while (i < n - 1 && arr[i] == arr[i + 1])
                i++;
      
            // print last occurrence of 
            // the current element
            Console.Write(arr[i] + " ");
        }
    }
      
    // Driver program 
    public static void Main () 
    {
        int []arr = {6, 10, 5, 4, 9, 120, 4, 6, 10};
        int n = arr.Length;
          
        printDistinct(arr, n);
    }
}
  
// This code is contributed by Sam007.

PHP


Javascript


输出:

4 5 6 9 10 120

我们可以使用哈希在平均 O(n) 时间内解决这个问题。这个想法是从左到右遍历给定的数组并跟踪哈希表中的访问元素。下面是这个想法的实现。

C++

/* CPP program to print all distinct elements 
   of a given array */
#include
using namespace std;
  
// This function prints all distinct elements
void printDistinct(int arr[],int n)
{
    // Creates an empty hashset
    unordered_set s;
  
    // Traverse the input array
    for (int i=0; i

Java

/* Java program to print all distinct elements of a given array */
import java.util.*;
  
class Main
{
    // This function prints all distinct elements
    static void printDistinct(int arr[])
    {
        // Creates an empty hashset
        HashSet set = new HashSet<>();
  
        // Traverse the input array
        for (int i=0; i

蟒蛇3

# Python3 program to print all distinct elements 
# of a given array 
  
# This function prints all distinct elements
def printDistinct(arr, n):
      
    # Creates an empty hashset
    s = dict();
  
    # Traverse the input array
    for i in range(n):
          
        # If not present, then put it in
        # hashtable and print it
        if (arr[i] not in s.keys()):
            s[arr[i]] = arr[i];
            print(arr[i], end = " ");
   
# Driver Code
arr = [10, 5, 3, 4, 3, 5, 6];
n = 7;
printDistinct(arr, n);
  
# This code is contributed by Princi Singh

C#

// C# program to print all distinct
// elements of a given array 
using System;
using System.Collections.Generic;
  
class GFG
{
// This function prints all 
// distinct elements 
public static void printDistinct(int[] arr)
{
    // Creates an empty hashset 
    HashSet set = new HashSet();
  
    // Traverse the input array 
    for (int i = 0; i < arr.Length; i++)
    {
        // If not present, then put it 
        // in hashtable and print it 
        if (!set.Contains(arr[i]))
        {
            set.Add(arr[i]);
            Console.Write(arr[i] + " ");
        }
    }
}
  
// Driver Code
public static void Main(string[] args)
{
    int[] arr = new int[] {10, 5, 3, 4, 3, 5, 6};
    printDistinct(arr);
}
}
  
// This code is contributed by Shrikant13

Javascript


输出:

10 5 3 4 6 

散列优于排序的另一个优点是,元素的打印顺序与它们在输入数组中的顺序相同。

另一种方法:
1.将所有输入的整数放入hashmap的key
2.在循环外打印keySet

Java

import java.util.HashMap;
public class UniqueInArray2 {
  
    public static void main(String args[])
  
    {
        int ar[] = { 10, 5, 3, 4, 3, 5, 6 };
        HashMap hm = new HashMap();
        for (int i = 0; i < ar.length; i++) {
            hm.put(ar[i], i);
        }
        // Using hm.keySet() to print output 
        // reduces time complexity. - Lokesh
        System.out.println(hm.keySet());
  
    }
  
}

C#

// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
public class UniqueInArray2 
{
  
    public static void Main(String []args)
  
    {
        int []ar = { 10, 5, 3, 4, 3, 5, 6 };
        Dictionary hm = new Dictionary();
        for (int i = 0; i < ar.Length; i++)
        {
            if(hm.ContainsKey(ar[i]))
                hm.Remove(ar[i]);
            hm.Add(ar[i], i);
        }
  
        // Using hm.Keys to print output 
        // reduces time complexity. - Lokesh
        var v = hm.Keys;
        foreach(int a in v)
            Console.Write(a+" ");
  
    }
  
}
  
/* This code contributed by PrinciRaj1992 */

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程