📜  数组中非重复(不同)元素的乘积

📅  最后修改于: 2021-04-29 08:14:10             🧑  作者: Mango

给定一个具有重复元素的整数数组。任务是找到给定数组中所有不同元素的乘积。

例子

一个简单的解决方案是使用两个嵌套循环。外循环从最左边的元素开始一个接一个地选择一个元素。内部循环检查元素的左侧是否存在。如果存在,则忽略该元素。

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

此问题的更好解决方案是首先按升序对数组的所有元素进行排序,然后在数组中逐个查找一个不同的元素。最后,找到所有不同元素的乘积。

以下是此方法的实现:

C++
// C++ program to find the product of all
// non-repeated elements in an array
  
#include 
using namespace std;
  
// Function to find the product of all
// non-repeated elements in an array
int findProduct(int arr[], int n)
{
    // sort all elements of array
    sort(arr, arr + n);
  
    int prod = 1;
    for (int i = 0; i < n; i++) {
        if (arr[i] != arr[i + 1])
            prod = prod * arr[i];
    }
  
    return prod;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 1, 1, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findProduct(arr, n);
  
    return 0;
}


Java
// Java program to find the product of all
// non-repeated elements in an array
import java.util.Arrays;
  
class GFG {
  
// Function to find the product of all
// non-repeated elements in an array
static int findProduct(int arr[], int n)
{
    // sort all elements of array
    Arrays.sort(arr);
      
    int prod = 1 * arr[0];
    for (int i = 0; i < n - 1; i++) 
    {
        if (arr[i] != arr[i + 1]) 
        {
            prod = prod * arr[i + 1];
        }
          
    }
    return prod;
}
  
// Driver code
public static void main(String[] args) {
    int arr[] = {1, 2, 3, 1, 1, 4, 5, 6};
    int n = arr.length;
    System.out.println(findProduct(arr, n));
    }
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python 3 program to find the product 
# of all non-repeated elements in an array
  
# Function to find the product of all
# non-repeated elements in an array
def findProduct(arr, n):
      
    # sort all elements of array
    sorted(arr)
  
    prod = 1
    for i in range(0, n, 1):
        if (arr[i - 1] != arr[i]):
            prod = prod * arr[i]
  
    return prod;
  
# Driver code
if __name__ == '__main__':
    arr = [1, 2, 3, 1, 1, 4, 5, 6]
  
    n = len(arr)
  
    print(findProduct(arr, n))
  
# This code is contributed by 
# Surendra_Gangwar


C#
// C# program to find the product of all
// non-repeated elements in an array
using System;
  
class GFG 
{
  
// Function to find the product of all
// non-repeated elements in an array
static int findProduct(int []arr, int n)
{
    // sort all elements of array
    Array.Sort(arr);
      
    int prod = 1 * arr[0];
    for (int i = 0; i < n - 1; i++) 
    {
        if (arr[i] != arr[i + 1]) 
        {
            prod = prod * arr[i + 1];
        }
          
    }
    return prod;
}
  
// Driver code
public static void Main() 
{
    int []arr = {1, 2, 3, 1, 1, 4, 5, 6};
    int n = arr.Length;
    Console.WriteLine(findProduct(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


CPP
// C++ program to find the product of all
// non- repeated elements in an array
#include 
using namespace std;
  
// Function to find the product of all
// non-repeated elements in an array
int findProduct(int arr[], int n)
{
    int prod = 1;
  
    // Hash to store all element of array
    unordered_set s;
    for (int i = 0; i < n; i++) {
        if (s.find(arr[i]) == s.end()) {
            prod *= arr[i];
            s.insert(arr[i]);
        }
    }
  
    return prod;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 1, 1, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findProduct(arr, n);
  
    return 0;
}


Java
// Java program to find the product of all
// non- repeated elements in an array
import java.util.HashSet;
  
class GFG 
{
  
    // Function to find the product of all
    // non-repeated elements in an array
    static int findProduct(int arr[], int n)
    {
        int prod = 1;
  
        // Hash to store all element of array
        HashSet s = new HashSet<>();
        for (int i = 0; i < n; i++) 
        {
            if (!s.contains(arr[i])) 
            {
                prod *= arr[i];
                s.add(arr[i]);
            }
        }
  
        return prod;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int arr[] = {1, 2, 3, 1, 1, 4, 5, 6};
        int n = arr.length;
  
        System.out.println(findProduct(arr, n));
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python
# Python3 program to find the product of all
# non- repeated elements in an array
  
# Function to find the product of all
# non-repeated elements in an array
def findProduct( arr, n):
  
    prod = 1
  
    # Hash to store all element of array
    s = dict()
    for i in range(n):
        if (arr[i] not in s.keys()):
            prod *= arr[i]
            s[arr[i]] = 1
      
    return prod
  
# Driver code
arr= [1, 2, 3, 1, 1, 4, 5, 6] 
n = len(arr)
  
print(findProduct(arr, n))
  
# This code is contributed by mohit kumar


C#
// C# program to find the product of all
// non- repeated elements in an array
using System;
using System.Collections.Generic; 
      
class GFG 
{
  
    // Function to find the product of all
    // non-repeated elements in an array
    static int findProduct(int []arr, int n)
    {
        int prod = 1;
  
        // Hash to store all element of array
        HashSet s = new HashSet();
        for (int i = 0; i < n; i++) 
        {
            if (!s.Contains(arr[i])) 
            {
                prod *= arr[i];
                s.Add(arr[i]);
            }
        }
  
        return prod;
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        int []arr = {1, 2, 3, 1, 1, 4, 5, 6};
        int n = arr.Length;
  
        Console.WriteLine(findProduct(arr, n));
    }
}
  
// This code is contributed by Princi Singh


输出:
720

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

一种有效的解决方案是遍历数组并保留哈希图,以检查元素是否重复。在遍历当前元素是否已经存在于哈希中时,如果是,则表示它被重复并且不应与乘积相乘;如果它不存在于哈希中,则将其与乘积相乘并将其插入哈希。

以下是此方法的实现:

CPP

// C++ program to find the product of all
// non- repeated elements in an array
#include 
using namespace std;
  
// Function to find the product of all
// non-repeated elements in an array
int findProduct(int arr[], int n)
{
    int prod = 1;
  
    // Hash to store all element of array
    unordered_set s;
    for (int i = 0; i < n; i++) {
        if (s.find(arr[i]) == s.end()) {
            prod *= arr[i];
            s.insert(arr[i]);
        }
    }
  
    return prod;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 1, 1, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findProduct(arr, n);
  
    return 0;
}

Java

// Java program to find the product of all
// non- repeated elements in an array
import java.util.HashSet;
  
class GFG 
{
  
    // Function to find the product of all
    // non-repeated elements in an array
    static int findProduct(int arr[], int n)
    {
        int prod = 1;
  
        // Hash to store all element of array
        HashSet s = new HashSet<>();
        for (int i = 0; i < n; i++) 
        {
            if (!s.contains(arr[i])) 
            {
                prod *= arr[i];
                s.add(arr[i]);
            }
        }
  
        return prod;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int arr[] = {1, 2, 3, 1, 1, 4, 5, 6};
        int n = arr.length;
  
        System.out.println(findProduct(arr, n));
    }
}
  
/* This code contributed by PrinciRaj1992 */

Python

# Python3 program to find the product of all
# non- repeated elements in an array
  
# Function to find the product of all
# non-repeated elements in an array
def findProduct( arr, n):
  
    prod = 1
  
    # Hash to store all element of array
    s = dict()
    for i in range(n):
        if (arr[i] not in s.keys()):
            prod *= arr[i]
            s[arr[i]] = 1
      
    return prod
  
# Driver code
arr= [1, 2, 3, 1, 1, 4, 5, 6] 
n = len(arr)
  
print(findProduct(arr, n))
  
# This code is contributed by mohit kumar

C#

// C# program to find the product of all
// non- repeated elements in an array
using System;
using System.Collections.Generic; 
      
class GFG 
{
  
    // Function to find the product of all
    // non-repeated elements in an array
    static int findProduct(int []arr, int n)
    {
        int prod = 1;
  
        // Hash to store all element of array
        HashSet s = new HashSet();
        for (int i = 0; i < n; i++) 
        {
            if (!s.Contains(arr[i])) 
            {
                prod *= arr[i];
                s.Add(arr[i]);
            }
        }
  
        return prod;
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        int []arr = {1, 2, 3, 1, 1, 4, 5, 6};
        int n = arr.Length;
  
        Console.WriteLine(findProduct(arr, n));
    }
}
  
// This code is contributed by Princi Singh
输出:
720

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