给定一个具有重复元素的整数数组。任务是找到给定数组中所有不同元素的乘积。
例子:
Input : arr[] = {12, 10, 9, 45, 2, 10, 10, 45, 10};
Output : 97200
Here we take 12, 10, 9, 45, 2 for product
because these are the only distinct elements
Input : arr[] = {1, 10, 9, 4, 2, 10, 10, 45, 4};
Output : 32400
一个简单的解决方案是使用两个嵌套循环。外循环从最左边的元素开始一个一个地选取一个元素。内循环检查元素是否存在于它的左侧。如果存在,则忽略该元素。
时间复杂度: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
Javascript
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
Javascript
输出:
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
Javascript
输出:
720
时间复杂度:O(N)
辅助空间:O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。