File tree 3 files changed +131
-1
lines changed
solution/1700-1799/1748.Sum of Unique Elements
3 files changed +131
-1
lines changed Original file line number Diff line number Diff line change @@ -89,6 +89,23 @@ class Solution {
89
89
}
90
90
```
91
91
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
+
92
109
### ** C++**
93
110
94
111
``` cpp
@@ -110,6 +127,24 @@ public:
110
127
};
111
128
```
112
129
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
+
113
148
### ** Go**
114
149
115
150
``` go
@@ -127,6 +162,21 @@ func sumOfUnique(nums []int) (ans int) {
127
162
}
128
163
```
129
164
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
+
130
180
### ** TypeScript**
131
181
132
182
``` ts
@@ -145,6 +195,21 @@ function sumOfUnique(nums: number[]): number {
145
195
}
146
196
```
147
197
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
+
148
213
### ** Rust**
149
214
150
215
``` rust
Original file line number Diff line number Diff line change @@ -74,6 +74,23 @@ class Solution {
74
74
}
75
75
```
76
76
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
+
77
94
### ** C++**
78
95
79
96
``` cpp
@@ -95,6 +112,24 @@ public:
95
112
};
96
113
```
97
114
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
+
98
133
### ** Go**
99
134
100
135
``` go
@@ -112,6 +147,21 @@ func sumOfUnique(nums []int) (ans int) {
112
147
}
113
148
```
114
149
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
+
115
165
### ** TypeScript**
116
166
117
167
``` ts
@@ -130,6 +180,21 @@ function sumOfUnique(nums: number[]): number {
130
180
}
131
181
```
132
182
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
+
133
198
### ** Rust**
134
199
135
200
``` rust
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ function sumOfUnique(nums: number[]): number {
5
5
}
6
6
let ans = 0 ;
7
7
for ( let x = 0 ; x < 101 ; ++ x ) {
8
- if ( cnt [ x ] == 1 ) {
8
+ if ( cnt [ x ] === 1 ) {
9
9
ans += x ;
10
10
}
11
11
}
You can’t perform that action at this time.
0 commit comments