Skip to content

Commit b9c774d

Browse files
committed
feat: add solutions to lc problem: No.1748
No.1748.Sum of Unique Elements
1 parent 320fcb5 commit b9c774d

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

solution/1700-1799/1748.Sum of Unique Elements/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ class Solution {
8989
}
9090
```
9191

92+
```java
93+
class Solution {
94+
public int sumOfUnique(int[] nums) {
95+
int ans = 0;
96+
int[] cnt = new int[101];
97+
for (int x : nums) {
98+
if (++cnt[x] == 1) {
99+
ans += x;
100+
} else if (cnt[x] == 2) {
101+
ans -= x;
102+
}
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
92109
### **C++**
93110

94111
```cpp
@@ -110,6 +127,24 @@ public:
110127
};
111128
```
112129
130+
```cpp
131+
class Solution {
132+
public:
133+
int sumOfUnique(vector<int>& nums) {
134+
int ans = 0;
135+
int cnt[101]{};
136+
for (int& x : nums) {
137+
if (++cnt[x] == 1) {
138+
ans += x;
139+
} else if (cnt[x] == 2) {
140+
ans -= x;
141+
}
142+
}
143+
return ans;
144+
}
145+
};
146+
```
147+
113148
### **Go**
114149

115150
```go
@@ -127,6 +162,21 @@ func sumOfUnique(nums []int) (ans int) {
127162
}
128163
```
129164

165+
```go
166+
func sumOfUnique(nums []int) (ans int) {
167+
cnt := [101]int{}
168+
for _, x := range nums {
169+
cnt[x]++
170+
if cnt[x] == 1 {
171+
ans += x
172+
} else if cnt[x] == 2 {
173+
ans -= x
174+
}
175+
}
176+
return
177+
}
178+
```
179+
130180
### **TypeScript**
131181

132182
```ts
@@ -145,6 +195,21 @@ function sumOfUnique(nums: number[]): number {
145195
}
146196
```
147197

198+
```ts
199+
function sumOfUnique(nums: number[]): number {
200+
let ans = 0;
201+
const cnt = new Array(101).fill(0);
202+
for (const x of nums) {
203+
if (++cnt[x] === 1) {
204+
ans += x;
205+
} else if (cnt[x] === 2) {
206+
ans -= x;
207+
}
208+
}
209+
return ans;
210+
}
211+
```
212+
148213
### **Rust**
149214

150215
```rust

solution/1700-1799/1748.Sum of Unique Elements/README_EN.md

+65
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ class Solution {
7474
}
7575
```
7676

77+
```java
78+
class Solution {
79+
public int sumOfUnique(int[] nums) {
80+
int ans = 0;
81+
int[] cnt = new int[101];
82+
for (int x : nums) {
83+
if (++cnt[x] == 1) {
84+
ans += x;
85+
} else if (cnt[x] == 2) {
86+
ans -= x;
87+
}
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
7794
### **C++**
7895

7996
```cpp
@@ -95,6 +112,24 @@ public:
95112
};
96113
```
97114
115+
```cpp
116+
class Solution {
117+
public:
118+
int sumOfUnique(vector<int>& nums) {
119+
int ans = 0;
120+
int cnt[101]{};
121+
for (int& x : nums) {
122+
if (++cnt[x] == 1) {
123+
ans += x;
124+
} else if (cnt[x] == 2) {
125+
ans -= x;
126+
}
127+
}
128+
return ans;
129+
}
130+
};
131+
```
132+
98133
### **Go**
99134

100135
```go
@@ -112,6 +147,21 @@ func sumOfUnique(nums []int) (ans int) {
112147
}
113148
```
114149

150+
```go
151+
func sumOfUnique(nums []int) (ans int) {
152+
cnt := [101]int{}
153+
for _, x := range nums {
154+
cnt[x]++
155+
if cnt[x] == 1 {
156+
ans += x
157+
} else if cnt[x] == 2 {
158+
ans -= x
159+
}
160+
}
161+
return
162+
}
163+
```
164+
115165
### **TypeScript**
116166

117167
```ts
@@ -130,6 +180,21 @@ function sumOfUnique(nums: number[]): number {
130180
}
131181
```
132182

183+
```ts
184+
function sumOfUnique(nums: number[]): number {
185+
let ans = 0;
186+
const cnt = new Array(101).fill(0);
187+
for (const x of nums) {
188+
if (++cnt[x] === 1) {
189+
ans += x;
190+
} else if (cnt[x] === 2) {
191+
ans -= x;
192+
}
193+
}
194+
return ans;
195+
}
196+
```
197+
133198
### **Rust**
134199

135200
```rust

solution/1700-1799/1748.Sum of Unique Elements/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function sumOfUnique(nums: number[]): number {
55
}
66
let ans = 0;
77
for (let x = 0; x < 101; ++x) {
8-
if (cnt[x] == 1) {
8+
if (cnt[x] === 1) {
99
ans += x;
1010
}
1111
}

0 commit comments

Comments
 (0)